Java || Greedy + Map + TreeSet || O(nlogn) || Single Pass
\n\tpublic int[] avoidFlood(int[] rains) {\n\n\t\tint len = rains.length;\n\t\tint[] ans = new int[len];\n\t\tHashMap map = new HashMap<>();\n\t\tTreeSet set = 
\n\tpublic int[] avoidFlood(int[] rains) {\n\n\t\tint len = rains.length;\n\t\tint[] ans = new int[len];\n\t\tHashMap<Integer, Integer> map = new HashMap<>();\n\t\tTreeSet<Integer> set = new TreeSet<>();\n\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tif (rains[i] != 0) {\n\t\t\t\tans[i] = -1;\n\t\t\t\tif (map.containsKey(rains[i])) {\n\t\t\t\t\tint day = map.get(rains[i]);\n\t\t\t\t\tInteger spot = set.ceiling(day);\n\t\t\t\t\tif (spot == null)\n\t\t\t\t\t\treturn new int[0];\n\t\t\t\t\tset.remove(spot);\n\t\t\t\t\tans[spot] = rains[i];\n\t\t\t\t\tmap.put(rains[i], i);\n\t\t\t\t} else\n\t\t\t\t\tmap.put(rains[i], i);\n\t\t\t} else {\n\t\t\t\tans[i] = 10;\n\t\t\t\tset.add(i);\n\t\t\t}\n\t\t}\n\t\treturn ans;\n\t}\n
[]