Java || Greedy + Map + Set + Heap || O(nlogn) || Double 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\tHashSet fille
\n\tpublic int[] avoidFlood(int[] rains) {\n\n\t\tint len = rains.length;\n\t\tint[] ans = new int[len];\n\t\tHashMap<Integer, ArrayDeque<Integer>> map = new HashMap<>();\n\t\tHashSet<Integer> filled = new HashSet<>();\n\t\tPriorityQueue<int[]> heap = new PriorityQueue<>(new Comparator<int[]>() {\n\t\t\t@Override\n\t\t\tpublic int compare(int[] o1, int[] o2) {\n\t\t\t\treturn o1[1] - o2[1];\n\t\t\t}\n\t\t});\n\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tif (rains[i] != 0) {\n\t\t\t\tArrayDeque<Integer> dequeue = map.getOrDefault(rains[i], new ArrayDeque<>());\n\t\t\t\tdequeue.offerLast(i);\n\t\t\t\tmap.put(rains[i], dequeue);\n\t\t\t}\n\t\t}\n\n\t\tfor (int key : map.keySet())\n\t\t\tmap.get(key).offerLast(Integer.MAX_VALUE);\n\n\t\tfor (int i = 0; i < len; i++) {\n\t\t\tif (rains[i] != 0) {\n\t\t\t\tif (filled.contains(rains[i]))\n\t\t\t\t\treturn new int[0];\n\t\t\t\tans[i] = -1;\n\t\t\t\tArrayDeque<Integer> dequeue = map.get(rains[i]);\n\t\t\t\tdequeue.pollFirst();\n\t\t\t\tint idx = dequeue.peekFirst();\n\t\t\t\theap.add(new int[] { rains[i], idx });\n\t\t\t\tfilled.add(rains[i]);\n\t\t\t} else {\n                int lake = (heap.size() != 0) ? heap.poll()[0] : 10;\n\t\t\t\tans[i] = lake;\n\t\t\t\tfilled.remove(lake);\n\t\t\t}\n\t\t}\n\n\t\treturn ans;\n\t}\n    \n
[]