Simple Java Solution using HashMap and TreeSet | Easy to Understand
Please upvote, if you find it useful :)\n\n\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        HashMap<Integer, Integer> map = new HashMap<>
Please upvote, if you find it useful :)\n\n```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        HashMap<Integer, Integer> map = new HashMap<>();\n        int[] ans = new int[rains.length];\n        TreeSet<Integer> tset = new TreeSet<>();\n\n        for(int i=0; i<rains.length; i++) {\n            if(rains[i] == 0) {\n                tset.add(i);\n            } else {\n                if(map.containsKey(rains[i])) {\n                    int idx = map.get(rains[i]);\n                    \n                    Integer ddidx = tset.ceiling(idx);\n                    if(ddidx == null) {\n                        return new int[]{};\n                    }\n\n                    ans[ddidx] = rains[i];\n                    tset.remove(ddidx);\n                    map.put(rains[i], i);\n                    \n                } else {\n                    map.put(rains[i], i);\n                }\n                ans[i] = -1;\n            }\n        }\n        \n        for(int i = 0; i<ans.length; i++) {\n            if(ans[i] == 0) {\n                ans[i] = 1;\n            }\n        }\n        \n        return ans;\n    }\n}\n```
['Tree', 'Ordered Set', 'Java']