Python-well explained & simple logic-O(nlogn)- greedy and heap
Variables:\n1. dic stores the raining day for each lake in ascending order.\n2. to_empty is a Min-heap and records the lakes that are full and sorted in urgency
Variables:\n1. ```dic``` stores the raining day for each lake in ascending order.\n2. ```to_empty``` is a Min-heap and records the lakes that are full and sorted in **urgency** order. Notably, lake ```i``` has the highest **urgency** means that lake ```i``` is currently full and will be rained in the most recent future among all other full lakes.\n3. ```full``` is a HashSet used to record the full lakes for efficient queries. ```lake in full``` could be replaced by ```lake in to_empty``` yet the time complexity is ```O(1)``` vs. ```O(n)```.\n\nLogics:\nWe dry lakes in the order of **urgency** - Greedy.\nIterating through days, when day ```i``` is raining on lake ```lake```, if ```lake``` is already full, simply return ```[]```; else, push the next raining day for ```lake``` to ```to_empty``` to queue it for drying.\nWhen day ```i``` is sunny, dry the most **urgent** lake referring to ```to_empty```. Remember to remove it from ```full```.\n\nPython code is as follows:\n```\nclass Solution:\n    def avoidFlood(self, rains: List[int]) -> List[int]:\n        dic = collections.defaultdict(list)\n        ret = [-1] * len(rains)\n        to_empty = [] # index\n        full = set([])\n        for day,lake in enumerate(rains):\n            dic[lake].append(day)\n        \n        for i in range(len(rains)):\n            lake = rains[i]\n            if lake:\n                if lake in full:\n                    return []\n                full.add(lake)\n                dic[lake].pop(0)\n                if dic[lake]:\n                    heapq.heappush(to_empty,dic[lake][0])\n            else:\n                if to_empty:\n                    ret[i] = rains[heapq.heappop(to_empty)]\n                    full.remove(ret[i])\n                else:\n                    ret[i] = 1\n        return ret\n```\n\nOr we can get rid of ```full``` and take advantage of ```dic```, not big difference tho.\n```\nclass Solution:\n    def avoidFlood(self, rains: List[int]) -> List[int]:\n        dic = collections.defaultdict(list)\n        ret = [-1] * len(rains)\n        to_empty = [] # index\n        \n        for day,lake in enumerate(rains):\n            dic[lake].append(day)\n        \n        for i in range(len(rains)):\n            lake = rains[i]\n            if lake:\n                if dic[lake] and dic[lake][0] < i:\n                    return []\n                if dic[lake] and len(dic[lake])>1:\n                    heapq.heappush(to_empty,dic[lake][1])\n            else:\n                if to_empty:\n                    ret[i] = rains[heapq.heappop(to_empty)]\n                    dic[ret[i]].pop(0)\n                else:\n                    ret[i] = 1\n        return ret\n```
['Greedy', 'Python']