Java: Solution with comments
\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer, Integer> map = new HashMap<>();\n        TreeSet<Integer> set = new TreeSet
```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer, Integer> map = new HashMap<>();\n        TreeSet<Integer> set = new TreeSet<>();\n        for (int i = 0; i < rains.length; i++) {\n\t\t//store the dryouts..which can be used later\n            if (rains[i] == 0) {\n                rains[i] = 1;\n                set.add(i);\n                continue;\n            }\n            //We have seen rain in this lake before..it needs to be dried out..else flood !!!!\n            if (map.containsKey(rains[i])) {\n\n                //if there are dryouts available.. try using the dryout now\n                if (set.size() > 0) {\n\n                    //we will try to find out any dryout periods which are available after rain in current lake\n                    Integer dryout = set.ceiling(map.get(rains[i]));\n\n                    //if all the dryout are before the rain happend in current lake\n                    //that means we cannot use them (it\'s a constraint)\n                    if(dryout == null)\n                        return new int[0];\n                    \n                    //update the dryout index with the lake\n                    rains[dryout] = rains[i];\n                    \n                    //put the new index of the lake\n                    //which means this lake is again full and needs to be dried out if it rains again \n                    map.put(rains[i], i);\n                    rains[i] = -1;\n\n                    //remove the used dryout\n                    set.remove(dryout);\n                } \n                else\n                    return new int[0];\n\n            } else {\n                map.put(rains[i], i);\n                rains[i] = -1;\n            }\n        }\n        return rains;\n    }\n}\n```
[]