{x}
blog image

Avoid Flood in The City

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.length
  • ans[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 <= 105
  • 0 <= rains[i] <= 109

Solution Explanation: Avoid Flood in The City

This problem asks us to schedule lake drying days to prevent floods, given a forecast of rainy days. The key is a greedy approach combined with efficient data structures.

Approach:

  1. Data Structures:

    • ans: An array to store the results. ans[i] = -1 if it's a rainy day, and ans[i] is the lake dried on day i if it's a sunny day.
    • rainy: A hash map (dictionary in Python) to store the last rainy day for each lake. This allows quick lookup to check if a lake has already been flooded.
    • sunny: A sorted set (or equivalent data structure) to keep track of sunny days. This allows efficient searching for the next suitable day to dry a specific lake.
  2. Greedy Strategy:

    • For each rainy day:
      • If the lake has rained before (checked using rainy), we need to find a sunny day to dry it before the next rain. We search for the earliest sunny day after the last rainy day for that lake using sunny.bisect_right() (Python) or sunny.higher() (Java/C++). If no such day exists, it's impossible to prevent a flood, so we return an empty array. We then update sunny by removing the chosen sunny day.
      • If it's the first rain for the lake, simply record the rainy day in rainy.
    • For each sunny day:
      • Add the day to the sunny set.
      • We can dry any lake, but for simplicity, the code chooses a lake that was last flooded (if any).
  3. Time and Space Complexity:

    • Time Complexity: O(N log N), where N is the length of rains. The dominant operations are insertions and searches in the sunny sorted set, which take O(log N) time each.
    • Space Complexity: O(N) to store ans, rainy, and sunny. In the worst case, all days could be sunny or all lakes could be rained on.

Code Examples (Python, Java, C++, Go, TypeScript):

The provided code snippets in different languages implement this greedy approach. The core logic is the same across all languages. The choice of data structures (sorted set, tree set, etc.) may vary slightly, but the algorithmic approach remains constant. The TypeScript example uses a custom-implemented Red-Black tree for the TreeSet, which is more complex than the built-in sets in the other languages but still maintains the logarithmic time complexity. Using a library such as lodash-es would provide similar functionality without needing to implement a Red-Black Tree manually.

Example Walkthrough (Example 2):

rains = [1, 2, 0, 0, 2, 1]

  1. Day 1: rains[0] = 1. rainy[1] = 0. ans = [-1, -1, -1, -1, -1, -1]
  2. Day 2: rains[1] = 2. rainy[2] = 1. ans = [-1, -1, -1, -1, -1, -1]
  3. Day 3: rains[2] = 0. sunny = {2}. ans = [-1, -1, 2, -1, -1, -1] (Dry lake 2)
  4. Day 4: rains[3] = 0. sunny = {2, 3}. ans = [-1, -1, 2, 1, -1, -1] (Dry lake 1)
  5. Day 5: rains[4] = 2. rainy[2] = 4. We search in sunny for a day after rainy[2] = 1. We find 2. ans = [-1, -1, 2, 1, -1, -1], sunny = {3}
  6. Day 6: rains[5] = 1. rainy[1] = 5. We search in sunny for a day after rainy[1] = 0. We find 3. ans = [-1, -1, 1, 1, -1, -1], sunny = {}

The final ans is [-1, -1, 2, 1, -1, -1], which is a valid solution that avoids floods. Note that other valid solutions exist.