Java solution with documented comments
java\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        // this map stores last day when it rained over lake\n        Map<Integer, Integer> 
```java\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        // this map stores last day when it rained over lake\n        Map<Integer, Integer> fullLakeLastDay = new HashMap<>();\n        // contains set of dry days on which no lake has been dried yet\n        TreeSet<Integer> dryDays = new TreeSet<>();\n        int[] result = new int[rains.length];\n        Arrays.fill(result, -1);\n        for (int i = 0; i < rains.length; i++) {\n            int rain = rains[i];\n            if (rain == 0) {\n                // dry day, put it into dry day set\n                dryDays.add(i);\n            } else {\n                if (fullLakeLastDay.containsKey(rain)) {\n                    // get first dry day after last day it rained over lake\n                    Integer dryDayToEmptyLake = dryDays.ceiling(fullLakeLastDay.get(rain));\n                    if (dryDayToEmptyLake == null) {\n                        // no dry day so there is no way to empty lake which is already filled\n                        return new int[0];\n                    } else {\n                        // found the dry day, empty the lake on that dry day\n                        dryDays.remove(dryDayToEmptyLake);\n                        result[dryDayToEmptyLake] = rain;\n                    }\n                }\n                // now fill the lake again\n                fullLakeLastDay.put(rain, i);\n            }\n        }\n        for(Integer dryDay: dryDays) {\n            result[dryDay] = 1;\n        }\n        return result;\n    }\n}\n```
[]