Problem Description
You are given an array rains where rains[i] > 0 indicates that it rains over the lake rains[i] on day i (which then becomes full) and rains[i] == 0 indicates a dry day when you can choose one lake to dry. The goal is to avoid any flood: if it rains on a lake that is already full, it will flood. You must decide on dry days which lake to remove water from so that no flood occurs. If a valid solution exists return an array indicating your choices (with -1 on rainy days and the lake number to dry on dry days). If it is impossible to prevent a flood, return an empty array.
Key Insights
- When it rains on a lake, mark that lake as full along with the current day index.
- If the same lake is rained upon again without being dried, a flood occurs.
- On a dry day (rains[i] == 0), choose to dry a lake that is scheduled to receive rain in the future and is currently full.
- Use a data structure to keep track of available dry days (and their indices) and search for a dry day that occurs after the previous rain for a given lake.
- A greedy algorithm with binary search (or a balanced tree structure) is a natural fit to make the optimal drying decision.
Space and Time Complexity
Time Complexity: O(n log n) – Each dry day lookup involves a binary search in the sorted list of dry day indices. Space Complexity: O(n) – To maintain the mapping of lakes to their last rain day and the list of available dry days.
Solution
The solution is based on a greedy strategy combined with binary search. We maintain:
- A dictionary (or hash table) to keep track of the last day each lake was rained upon.
- A sorted list (or tree set) to store indices of days where no rain occurs (dry days).
For each day:
- If it rains (rains[i] > 0):
- If the lake is already full (exists in our dictionary), we try to find the earliest dry day (using binary search) that is after the previous rain day for that lake.
- If such a dry day does not exist, a flood is inevitable, so we return an empty array.
- Otherwise, we assign that dry day to dry the lake and update our data structures.
- If it is a dry day, we simply add the day's index to our sorted list of available dry days and, by default, assign an arbitrary value (e.g., 1) in the result. This value will be overridden if this day gets used to dry a specific lake.
This approach ensures that we always use the best available dry day for a lake that might cause a future flood.