JAVA Short and Easy to Understand Solution
Time Complexity: O(N * LogN). Space Complexity: O(N)\n```\n    public int[] avoidFlood(int[] rains) {\n        int n = rains.length;\n        int[] res = new in
Time Complexity: O(N * LogN). Space Complexity: O(N)\n```\n    public int[] avoidFlood(int[] rains) {\n        int n = rains.length;\n        int[] res = new int[n];\n        // TreeSet to save all the days that can dry the lake\n        TreeSet<Integer> days = new TreeSet();\n        // Set to save all the full lake\n        Map<Integer, Integer> map = new HashMap();\n        for (int i = 0; i < n; i++) {\n            int c = rains[i];\n            if (c == 0) {\n                days.add(i);\n                res[i] = 1;\n            } else {\n                res[i] = -1;\n                if (map.containsKey(c)) {\n                    Integer d = days.ceiling(map.get(c));\n                    if (d == null) return new int[]{};\n                    days.remove(d);\n                    res[d] = c; \n                } \n                map.put(c, i);\n            }\n        }\n        return res;\n    }
['Java']