Java Map + TreeSet solution
\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        int n = rains.length;\n        int[] res = new int[n];\n        Map<Integer, Integer> ma
```\nclass Solution {\n    public int[] avoidFlood(int[] rains) {\n        int n = rains.length;\n        int[] res = new int[n];\n        Map<Integer, Integer> map = new HashMap<>();\n        TreeSet<Integer> ts = new TreeSet<>();\n        for(int i = 0; i < n; i++) {\n            if(rains[i] != 0) {\n                res[i] = -1;\n                if(map.containsKey(rains[i])) {\n                    int index = map.get(rains[i]);\n                    if(ts.isEmpty() || ts.ceiling(index) == null) return new int[0];\n                    res[ts.ceiling(index)] = rains[i];\n                    map.put(rains[i], i);\n                    ts.remove(ts.ceiling(index));\n                } else {\n                    map.put(rains[i], i);\n                }\n            } else {\n                ts.add(i);\n            }\n        }\n        \n        for(int i = 0; i < n; i++) {\n            if(res[i] == 0) res[i] = 1;\n        }       \n        return res;\n    }\n}\n```
['Java']