Java | Time : O(nlogn)| Space : O(n) | Hash, TreeSet
\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer,Integer> rain = new HashMap<>();\n        TreeSet<Integer> dry = new TreeSet
```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer,Integer> rain = new HashMap<>();\n        TreeSet<Integer> dry = new TreeSet<>();\n        \n        int[] result = new int[rains.length];\n        Arrays.fill(result,-1);\n        \n        for(int i=0;i<rains.length;i++){\n            if(rains[i]==0){\n                dry.add(i);\n                result[i] = 1;\n            }else{\n                if(rain.containsKey(rains[i])){\n                    int day = rain.get(rains[i]);\n                    Integer dryDayCandidate = dry.ceiling(day);\n                    if(dryDayCandidate==null){\n                        return new int[]{};\n                    }else{\n                        result[dryDayCandidate] = rains[i];\n                        dry.remove(dryDayCandidate);\n                    }\n                }\n                rain.put(rains[i],i);\n            }\n        }\n        return result;\n    }\n}\n\n\n\n//[1]\n//[1,2,0,3,2,0,3,4,0,2,1]\n// 0 1 2 3 4 5 6 7 8 9 10\n\n//         Map                                        Zeros\n//i = 0, [1:0]\n//i = 1, [1:0, 2:1]\n//i = 2, [1:0, 2:1]                                     2\n//i = 3, [1:0, 2:1, 3:3]                                2\n//i = 4, rain = 2, we have in map,\n//       map.get(rain) = 1, it is filled\n//       at day 1, and in Zeros, we have 2\n//       which means at day 2, we can empty\n//       rain = 2, therefore, result[2] = rain         emtpy\n//       [1:0, 2:4, 3:3]\n//i = 5,                                                5\n//i = 6, rain = 3, we have it in map\n//       map.get(rain) = 3, and in Zeros, we\n//       have 5, therefore, result[5] = rain = 3      empty\n//       [1:0, 2:4, 3:6]\n//i = 7, [1:0, 2:4, 3:6, 4:7]\n//i = 8,                                                8\n//i = 9, rain = 2, we have it in map\n//       map.get(rain) = 4, and in zero, we have\n//       8, therefore, result[8] = rain = 4\n//       [1:0, 2:9, 3:6, 4:7]                         empty\n//i=10,  rain = 1, we have it in map\n//       map.get(rain) = 0, we don\'t have days in\n//       zeros to empty it, so we return empty result\n```
['Binary Search Tree']