题目描述:
We are given an elevation map, heights[i] representing the height of the terrain at that index. The width at each index is 1. After V units of water fall at index K, how much water is at each index?
Water first drops at index K and rests on top of the highest terrain or water at that index. Then, it flows according to the following rules:
If the droplet would eventually fall by moving left, then move left.
Otherwise, if the droplet would eventually fall by moving right, then move right.
Otherwise, rise at it’s current position.
Here, “eventually fall” means that the droplet will eventually be at a lower level if it moves in that direction. Also, “level” means the height of the terrain plus any water in that column.
We can assume there’s infinitely high terrain on the two sides out of bounds of the array. Also, there could not be partial water being spread out evenly on more than 1 grid block - each unit of water has to be in exactly one block.
Example 1:
Input: heights = [2,1,1,2,1,2,2], V = 4, K = 3
Output: [2,2,2,3,2,2,2]
Example 2:
Input: heights = [1,2,3,4], V = 2, K = 2
Output: [2,3,3,4]
Explanation:
The last droplet settles at index 1, since moving further left would not cause it to eventually fall to a lower height.
Example 3:
Input: heights = [3,1,3], V = 5, K = 1
Output: [4,4,4]
Note:
- heights will have length in [1, 100] and contain integers in [0, 99].
- V will be in range [0, 2000].
- K will be in range [0, heights.length - 1].
方法1:
优先队列
class Solution {
public int[] pourWater(int[] heights, int V, int K) {
Deque<Integer> leftFall = new ArrayDeque<>(); // [0, K)
Deque<Integer> rightFall = new ArrayDeque<>(); // [K, len]
int leftBoundary = K-1;
int rightBoundary = K;
int len = heights.length;
for (int step = 0; step < V; ++step) {
// scan left boundary H < VH add lower index
while(leftBoundary >= 0 && heights[leftBoundary] <= heights[leftBoundary+1]){
if(leftBoundary == K-1 || heights[leftBoundary] < heights[leftBoundary + 1]){
leftFall.push(leftBoundary);
}
--leftBoundary;
}
// scan right boundary H < VH add lower index
while(rightBoundary < len && (rightBoundary == K || heights[rightBoundary - 1] >= heights[rightBoundary])){
if( rightBoundary == K || heights[rightBoundary] < heights[rightBoundary-1]){
rightFall.push(rightBoundary);
}
++rightBoundary;
}
if(!leftFall.isEmpty() && heights[leftFall.peek()] < heights[K]){
int leftmostFall = leftFall.peek();
++heights[leftmostFall];
// Two possible consequences:
// 1. heights[idx] become equal to heights[idx + 1] => should not include [idx]
// in the stack anymore. (if idx + 1 is also a number in left, i.e. idx + 1 < K)
// 2. heights[idx] become strictly greater than heights[idx-1] => should include
// [idx - 1] in the stack.
if(leftmostFall < K-1 && heights[leftmostFall] == heights[leftmostFall + 1]){
leftFall.pop();
}
if(leftmostFall > 0 && heights[leftmostFall] > heights[leftmostFall-1]){
leftFall.push(leftmostFall-1);
}
}else{
int rightmostFall = rightFall.peek();
++heights[rightmostFall];
if(rightmostFall-1 >= K && heights[rightmostFall] == heights[rightmostFall - 1]){
rightFall.pop();
}
if(rightmostFall < len-1 && heights[rightmostFall] >heights[rightmostFall+1] ){
rightFall.push(rightmostFall+1);
}
}
}
return heights;
}
}