greedy with a heap
\nclass Solution:\n    def avoidFlood(self, rains: List[int]) -> List[int]:\n        seen = set()\n        closest = []\n        locs = collections.defaultdict(
```\nclass Solution:\n    def avoidFlood(self, rains: List[int]) -> List[int]:\n        seen = set()\n        closest = []\n        locs = collections.defaultdict(collections.deque)\n        for i, lake in enumerate(rains):\n            locs[lake].append(i)\n        ret = []\n        for lake in rains:\n            if lake in seen:\n                return []\n            if not lake:\n                # get closest that\'s already seen\n                if not closest:\n                    # there\'s nothing filled that will be filled again later\n                    ret.append(1) \n                    continue\n                nxt = heapq.heappop(closest)\n                ret.append(rains[nxt])\n                seen.remove(rains[nxt])\n            else:\n                seen.add(lake)\n                l = locs[lake]\n                l.popleft()\n                if l:\n                    nxt = l[0]\n                    heapq.heappush(closest, nxt)\n                ret.append(-1)\n        return ret\n```\n\nstart by getting all the days that each river is rained on, in chronological order.\n\nthen, go through each day. if a lake is rained on during a day, we get the next day that that river is going to be rained on in the future. we put this day in our "closest" minheap so that the next time we have a dry day, we can just pop from the minheap and fill up the lake that will be re-rained on nearest in the future.\n\nedit: solution without seen, thanks @yaroslav-repeta\n\n```\nclass Solution:\n    def avoidFlood(self, rains: List[int]) -> List[int]:\n        closest = []\n        locs = collections.defaultdict(collections.deque)\n        for i, lake in enumerate(rains):\n            locs[lake].append(i)\n        ret = []\n        for i, lake in enumerate(rains):\n            if closest and closest[0] == i:\n                return []\n            if not lake:\n                if not closest:\n                    ret.append(1) \n                    continue\n                nxt = heapq.heappop(closest)\n                ret.append(rains[nxt])\n            else:\n                l = locs[lake]\n                l.popleft()\n                if l:\n                    nxt = l[0]\n                    heapq.heappush(closest, nxt)\n                ret.append(-1)\n        return ret\n```
[]