Java Simple TreeSet and HashMap
See comments for details\n\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer, Integer> full = new HashMap<>();  // last days that is full\n      
See comments for details\n```\n    public int[] avoidFlood(int[] rains) {\n        Map<Integer, Integer> full = new HashMap<>();  // last days that is full\n        TreeSet<Integer> drain = new TreeSet<>();  // storage days to be used for drain\n        int n = rains.length, res[] = new int[n];\n        Arrays.fill(res, 1);  // in case extra draining days not used;\n        for (int i = 0; i < rains.length; i++) {\n            int lake = rains[i];\n            if (full.containsKey(lake)){\n                Integer key = drain.ceiling(full.get(lake));  // find if there is a day could be drain after last full\n                if (key == null) return new int[]{};  // did not find, flooded\n                res[key] = lake;    // set the day to be drained with lake\n                drain.remove(key);  // the darining day has already been used;\n            }\n            if (lake == 0) drain.add(i);  // we got new days\n            else {\n                full.put(lake, i);\n                res[i] = -1;  // lake is 0, or could be dry\n            }\n        }\n        return res;\n    }\n```
[]