Avoid Flood in The City Problem
Description
LeetCode Problem 1488.
Your country has 10^9 lakes. Initially, all the lakes are empty, but when it rains over the n^th lake, the n^th lake becomes full of water. If it rains over a lake that is full of water, there will be a flood. Your goal is to avoid floods in any lake.
Given an integer array rains where:
rains[i] > 0means there will be rains over therains[i]lake.rains[i] == 0means there are no rains this day and you must choose one lake this day and dry it.
Return an array ans where:
ans.length == rains.lengthans[i] == -1ifrains[i] > 0.ans[i]is the lake you choose to dry in theithday ifrains[i] == 0.
If there are multiple valid answers return any of them. If it is impossible to avoid flood return an empty array.
Notice that if you chose to dry a full lake, it becomes empty, but if you chose to dry an empty lake, nothing changes.
Example 1:
Input: rains = [1,2,3,4]
Output: [-1,-1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day full lakes are [1,2,3]
After the fourth day full lakes are [1,2,3,4]
There's no day to dry any lake and there is no flood in any lake.
Example 2:
Input: rains = [1,2,0,0,2,1]
Output: [-1,-1,2,1,-1,-1]
Explanation: After the first day full lakes are [1]
After the second day full lakes are [1,2]
After the third day, we dry lake 2. Full lakes are [1]
After the fourth day, we dry lake 1. There is no full lakes.
After the fifth day, full lakes are [2].
After the sixth day, full lakes are [1,2].
It is easy that this scenario is flood-free. [-1,-1,1,2,-1,-1] is another acceptable scenario.
Example 3:
Input: rains = [1,2,0,1,2]
Output: []
Explanation: After the second day, full lakes are [1,2]. We have to dry one lake in the third day.
After that, it will rain over lakes [1,2]. It's easy to prove that no matter which lake you choose to dry in the 3rd day, the other one will flood.
Constraints:
1 <= rains.length <= 10^50 <= rains[i] <= 10^9
Solution
Approach: Greedy, Dry on the Earliest Usable Dry Day
A flood happens when it rains on a lake that is already full. So whenever a lake gets rain for the second time, we must have dried it on some dry day between its previous rain and today. Drying it before the previous rain is useless, because that rain fills it again.
We process the days in order and keep:
lastRain[lake]: the last day it rained on each lake;- the dry days seen so far that have not been assigned yet.
On a dry day we just remember it (and tentatively answer 1, any lake, in case it is never needed). On a rainy day for a lake that is already full, we pick the first unused dry day after lastRain[lake] and use it to dry this lake. If there is none, a flood cannot be avoided and we return an empty array.
Why the first one? All the dry days after the previous rain work equally well for this lake, but an earlier dry day is usable by fewer future lakes (a lake can only use dry days after its own last rain). Spending the earliest one keeps the later, more flexible days available.
Finding the first unused dry day. The dry days are recorded in increasing order, so a binary search finds the first one after lastRain[lake]. That one may already be used, so we also keep a small union-find style array nxt: when the p-th dry day is used we set nxt[p] = p + 1, and following nxt (with path compression) jumps over used days to the first unused one.
Unused dry days keep the answer 1; drying an empty lake does nothing, so it is always allowed.
Complexity: O(n log n) time and O(n) extra space.
from bisect import bisect_right
class Solution:
def avoidFlood(self, rains: List[int]) -> List[int]:
ans = [-1] * len(rains)
dry_days = [] # indices of the dry days so far, increasing
nxt = [] # nxt[p] == p while dry_days[p] is unused; otherwise it points further right
last_rain = {} # lake -> the last day it rained there
def first_unused(p):
root = p
while root < len(nxt) and nxt[root] != root:
root = nxt[root]
while p != root: # path compression
nxt[p], p = root, nxt[p]
return root
for day, lake in enumerate(rains):
if lake == 0:
dry_days.append(day)
nxt.append(len(nxt))
ans[day] = 1 # dry some lake for now; may be replaced below
else:
if lake in last_rain:
# Dry this lake on the first unused dry day after it last rained.
p = first_unused(bisect_right(dry_days, last_rain[lake]))
if p == len(dry_days):
return []
ans[dry_days[p]] = lake
nxt[p] = p + 1
last_rain[lake] = day
return ans