Mathematical use of max heap O(nlogn), O(n) space. Speed > 88% and memory < 100%
So, maintain the minheap of next day for each day, same lake to be flooded. If the same lake is not going to be filled again, put next of that day as n(length o
So, maintain the minheap of next day for each day, same lake to be flooded. If the same lake is not going to be filled again, put next of that day as n(length of rains) (case of no flood). Put this in the minheap and on a dry day(no pun inteded), pop the lake from heap which will be filled again first.\n\n```\ndef avoidFlood(self, rains: List[int]) -> List[int]:\n## this is the minheap\n        arr = []\n##this map contains if an element is already filled\n        arrlis = {}\n##output arr\n        op = []\n##next time rain willl happen over ith lake\n        nxt = {}\n\t\t\n        for x in range(len(rains)):\n            i = rains[x]\n            if i in arrlis:\n                nxt[arrlis[i]] = x\n            arrlis[i] = x\n            \n        arrlis = {}\n            \n        for x in range(len(rains)):\n            i = rains[x]\n            if i > 0:\n                if i in arrlis:\n                    return []\n                nx = len(rains)\n                if x in nxt:\n                    nx = nxt[x]\n                heapq.heappush(arr, (nx , i))\n                arrlis[i] = x\n                op.append(-1)\n            else:\n                if len(arr) > 0:\n                    op.append(heapq.heappop(arr)[1])\n                    del arrlis[op[-1]]\n                else:\n                    op.append(5)\n                    if 5 in arrlis:\n                        del arrlis[5]\n                    \n        return op\n```
[]