[Java] nlog(n) find zero that can be used to empty the fullfilled lake
java\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        // the previous appeared idx of rains[i]\n        Map<Integer, Integer> map = new Ha
```java\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        // the previous appeared idx of rains[i]\n        Map<Integer, Integer> map = new HashMap<>();\n        TreeSet<Integer> zeros = new TreeSet<>();\n        int[] res = new int[rains.length];\n        for (int i = 0; i < rains.length; i++) {\n            if (rains[i] == 0) {\n                zeros.add(i);\n            } else {\n                if (map.containsKey(rains[i])) {\n                    // find the location of zero that can be used to empty rains[i]\n                    Integer next = zeros.ceiling(map.get(rains[i]));\n                    if (next == null) return new int[0];\n                    res[next] = rains[i];\n                    zeros.remove(next);\n                }\n                res[i] = -1;\n\t\t\t\tmap.put(rains[i], i);\n            }\n        }\n        for (int i : zeros) res[i] = 1;\n        return res;\n    }\n}\n```
['Tree', 'Ordered Set', 'Java']