Your country has an infinite number of lakes. Initially, all the lakes are empty, but when it rains over the nth lake, the nth 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] > 0 means there will be rains over the rains[i] lake.rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it.Return an array ans where:
ans.length == rains.lengthans[i] == -1 if rains[i] > 0.ans[i] is the lake you choose to dry in the ith day if rains[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 <= 1050 <= rains[i] <= 109The key challenge in #1488 Avoid Flood in The City is preventing any lake from flooding when it rains multiple times on the same lake. When a lake receives rain while it is already full, it must have been dried on a previous dry day. This naturally leads to a greedy strategy.
Track the last day each lake was filled using a HashMap. Whenever a lake rains again, you must find a dry day that occurs after the previous rain on that lake. To efficiently locate such a day, maintain a sorted structure (such as a TreeSet or binary-searchable list) that stores all available dry days.
When rain occurs on a previously filled lake, perform a binary search to find the earliest dry day after the last rain and assign it to dry that lake. If none exists, flooding is unavoidable. Otherwise, update the structures and continue processing. This greedy scheduling ensures each dry day is used optimally.
The approach runs in O(n log n) time due to ordered searches, with additional O(n) space for tracking lakes and dry days.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Greedy with HashMap and Binary Search (TreeSet of dry days) | O(n log n) | O(n) |
| Heap/Priority Queue Based Scheduling Variant | O(n log n) | O(n) |
Dave Burji
Use these hints if you're stuck. Try solving on your own first.
Keep An array of the last day there was rains over each city.
Keep an array of the days you can dry a lake when you face one.
When it rains over a lake, check the first possible day you can dry this lake and assign this day to this lake.
Watch expert explanations and walkthroughs
Practice problems asked by these companies to ace your technical interviews.
Explore More ProblemsJot down your thoughts, approach, and key learnings
Yes, this problem reflects common interview themes such as greedy scheduling, data structure selection, and handling constraints efficiently. Variations of similar scheduling and resource allocation problems frequently appear in FAANG-style interviews.
Binary search helps quickly find the earliest dry day that occurs after the last time a lake received rain. Since dry days must be used in the correct order, an ordered structure enables efficient lookups and keeps the algorithm within O(n log n) time.
A combination of HashMap and a sorted structure like a TreeSet works best. The HashMap tracks the last day each lake was filled, while the TreeSet stores available dry days and supports efficient binary search to assign drying operations.
The optimal approach uses a greedy strategy combined with a HashMap and an ordered set of dry days. When a lake rains again, we locate the earliest available dry day after its previous rain using binary search. This ensures lakes are dried only when necessary and prevents flooding.