6. The city has n lakes, numbered from 1 to n. Whenever it rains over a lake, that lake…
- User
- The city has n lakes, numbered from 1 to n. Whenever it rains over a lake, that lake becomes full. The problem is represented by an array called rains where rains[i] means it rains over the rains[i]
th lake on i
th day. If it rains on a lake that is already full, it results in a flood. To prevent flooding, you have the option to dry one lake on days when it does not rain. • rains[i] > 0 means there will be rains over the lake indexed by rains[i]. • rains[i] == 0 means there are no rains this day and you can choose one lake this day and dry it. Objective: Determine whether it is possible to prevent flooding. If flooding can be avoided, return an array where: • The elements corresponding to rainy days are set to 1. • On non-rainy days, return a positive integer representing the lake that is dried. If it is impossible to prevent flooding, return an empty array. Design an algorithm for the same and give its proof of correctness. Example 1: Input: rains = [1, 2, 3, 4] Output: [-1, -1, -1, -1] Explanation: No lakes are full, so no drying is needed. Example 2: Input: rains = [1, 2, 0, 0, 2, 1] Output: [-1, -1, 2, 1, -1, -1] Explanation: Dry lake 2 on day 3, and dry lake 1 on day 4 to avoid flooding.
- Assistant