Avoid Flood in The City

update Jun 23, 2020

Leetcodearrow-up-right

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] > 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:

  1. ans.length == rains.length

  2. ans[i] == -1 if rains[i] > 0.

  3. ans[i] is the lake you choose to dry in the ith day if rains[i] == 0.

  4. 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. 1 <= rains.length <= 10^5

  2. 0 <= rains[i] <= 10^9

Basic Idea

这道题乍看复杂,输入数组长度10e5,所以必须用低于N^2时间复杂度的算法才能通过。首先想到的是扫描,但问题是当我们遇到不降雨的那天该选择抽空哪个湖的水,一个贪心的思路就是我们选择当前满水的湖中最先会再次被降雨的湖来抽干。

于是我们需要有一个办法很快得知每个湖再次降雨的时间,然后在从左往右扫描的过程中,每次都选择前面下过雨的湖中再次降雨最早的湖来抽干。于是我们可以使用一个PriorityQueue。而至于如何得到每个雨天该湖再次下雨的时间,可以从后往前扫描,用一个HashMap来维持每个湖上次降雨的时间,再用一个next数组来跟踪每个雨天对应的湖下次下雨的时间。

Java Code:

Last updated