My Java Solution using HashMap, TreeSet O(nlogn)
\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        int n = rains.length;\n        int [] result = new int [n];\n        Arrays.fill(result,
```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        int n = rains.length;\n        int [] result = new int [n];\n        Arrays.fill(result, 1);\n        TreeSet<Integer> emptyLake = new TreeSet<>(); // O(logn)\n        Map<Integer, Integer> fullLake = new HashMap<>();\n        for (int i=0; i<n; i++) {\n            int lake = rains[i];\n            if (lake > 0) {\n                result[i] = -1;\n                if (fullLake.containsKey(lake)) {\n                    Integer emptyDate = emptyLake.higher(fullLake.get(lake));\n                    if (emptyDate == null)\n                        return new int [0];\n                    result[emptyDate] = lake;\n                    emptyLake.remove(emptyDate);\n                }\n                    fullLake.put(lake, i);\n            }\n            else {\n                emptyLake.add(i);\n            }\n        }\n        return result;\n    }\n}\n```
['Tree', 'Ordered Set', 'Java']