Sliding Window + Binary search (n log n)
```\n fun avoidFlood(rains: IntArray): IntArray {\n        val fullLakes = HashMap()\n        var emptyLakes = ArrayList()\n        var ans = IntArray(rains.siz
```\n fun avoidFlood(rains: IntArray): IntArray {\n        val fullLakes = HashMap<Int, Int>()\n        var emptyLakes = ArrayList<Int>()\n        var ans = IntArray(rains.size) { -1 }\n        \n        for(i in 0 until rains.size) {\n\t\t    if(rains[i] == 0) {\n\t\t\t   // if empty lake, save it for future filling.\n\t\t\t   emptyLakes.add(i)\n\t\t\t   continue\n\t\t\t}\n             if(fullLakes.containsKey(rains[i])) {\n\t\t\t    // NOTE : you can use Treemap here to make this step easy. but i like raw code than data structure.\n\t\t\t\t\n\t\t\t    // Do binary search to find the lake which is empty that comes after the day\n\t\t\t\t// when it rained at rains[i] lake previously\n\t\t\t\tvar start = 0\n\t\t\t\tvar end = emptyLakes.size - 1\n\t\t\t\tvar index = -1 \n\t\t\t\twhile(start <= end) {\n\t\t\t\t\tval mid = (start + end)/2\n\t\t\t\t\tif(emptyLakes[mid] > fullLakes[rains[i]]!!) {\n\t\t\t\t\t\tindex = mid\n\t\t\t\t\t\tend = mid - 1\n\t\t\t\t\t} else {\n\t\t\t\t\t\tstart = mid + 1\n\t\t\t\t\t}\n\t\t\t\t}\n\t\t\t\t// if no lake found, return empty \n\t\t\t\tif(index == -1) return intArrayOf()\n\t\t\t\t// if found, then empty rain[i] lake\n\t\t\t\tans[emptyLakes[index]] = rains[i]\n\t\t\t\t// remove empty lake now\n\t\t\t\temptyLakes.removeAt(index)\n\t\t\t}\n\t\t\t// Now update the day when it rained in rain[i] lake\n\t\t\tfullLakes[rains[i]] = i\n        }\n        for(l in emptyLakes) {\n\t\t    // fill all empty lakes with some numbers\n            ans[l] = 1\n        }\n        return ans\n    }
[]