1488. Avoid Flood in The City
https://leetcode.com/problems/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 which is full of water, there will be a flood. Your goal is to avoid the flood 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 can 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. (see example 4)
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:
Example 3:
Example 4:
Example 5:
Constraints:
1 <= rains.length <= 10^50 <= rains[i] <= 10^9
给定数组的元素值nums[i] 为[1, n]时表示第i天湖nums[i]会降雨,为0时表示该天为晴,可以选任意一个湖抽水,当一个湖有两次降水且中间没有抽过时会溢出,问如何抽水能防止出现溢出。贪心,对每一个0,选它后面最近的且需要抽水的湖。选最近用min heap。将每个湖所下雨的天数提前记下,每次遇到一个湖,把它的下一个雨天放入heap中。
Last updated
Was this helpful?