Java easy to understand - Time 63ms O(nlogn) Space O(n)
Algorithm\n  Start with the ans array equal to the length of he rains array\n  Fill the ans array with 1, this is defaulted if you have lot of dry day and no la
**Algorithm**\n*  Start with the `ans` array equal to the length of he `rains` array\n*  Fill the `ans` array with `1`, this is defaulted if you have lot of dry day and no lakes available at hand to empty\n*  You also have to maintain a `emptyLakes` **TreeSet** to store all the empty lakes that we have seen\n*  Maintain a `fullLakes` **HashMap** to track the full takes that we have seein in the rains array, key will be the rain and value will be the index.\n*  Loop through the rains array. \n*  If you see dry day .ie. value `0` for `rains[i]` then add the current index to the `emptyLakes`\n*  If you see `rain[i]` with `> 0` then fill it immediately with `-1`\n*  If you find the current lake in the `fullLakes` then get the `empty lake` from `emptyLakes` after the index of the full lake. This is because the dry day should come after the lake is first filled. You cannot fill with a lake that never got full.\n*  If you dont find any of the fullLakes then you can immediately return `[]`\n*  Otherwise, store the current lake to the `ans` and remove the lake from `emptyLakes` since its no more empty.\n*  Update the `fullLakes` with the current lake and index\n\n```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        int length  = rains.length;\n        int[] ans = new int[length];\n        Arrays.fill(ans, 1);\n        \n        TreeSet<Integer> emptyLakes  = new TreeSet<>();\n        Map<Integer,Integer> fullLakes  = new HashMap<>();\n        for (int i = 0;i<length;i++){\n            int lake = rains[i];\n            \n            if (lake > 0){       \n                ans[i] = -1;\n                \n                if (fullLakes.containsKey(lake)){\n                    Integer emptyDay = emptyLakes.higher(fullLakes.get(lake));\n                    if (emptyDay == null)\n                        return new int[0];\n                    \n                    ans[emptyDay] = lake;\n                    emptyLakes.remove(emptyDay);\n                }\n                \n                fullLakes.put(lake, i);\n            }\n            else{\n                emptyLakes.add(i);\n            }\n            \n        }\n    \n        return ans;\n    }\n}\n```
[]