HashMap + TreeSet Solution | Java | Easy
\n\n# Code\n\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        HashMap<Integer , Integer>lastRain = new HashMap<>();\n        TreeSet<Integ
\n\n# Code\n```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        HashMap<Integer , Integer>lastRain = new HashMap<>();\n        TreeSet<Integer>dry = new TreeSet<>();\n        int n = rains.length;\n        int[] ans = new int[n];\n\n        for(int i = 0; i < n; i++){\n            int x = rains[i];\n\n            if(x == 0){\n                dry.add(i);\n                ans[i] = 1;\n            }\n            else{\n                Integer id = lastRain.get(x);\n\n                if(id == null){\n                    lastRain.put(x , i);\n                }\n                else{\n                    Integer dryingDay = dry.higher(id);\n\n                    if(dryingDay == null){\n                        return new int[0];\n                    }\n                    ans[dryingDay] = x;\n                    dry.remove(dryingDay);\n\n                    lastRain.put(x , i);\n                }\n                ans[i] = -1;\n            }\n        }\n        return ans;\n    }\n}\n```
['Hash Table', 'Binary Search Tree', 'Java']