Leetcode 755: 倒水 Pour Water

给定地形数组heights,表示每个位置的海拔高度,宽度为1。当V单位的水落在位置K时,水会如何分布?水首先落在位置K的最高地形上,然后根据以下规则流动:向左移动直到下降,向右移动直到下降,否则在当前位置上升。返回每位置水的高度。例如,heights=[2,1,1,2,1,2,2],V=4,K=3,输出为[2,2,2,3,2,2,2]。注意,高度数组长度在[1, 100],V范围在[0, 2000],K在[0, heights.length - 1]。" 111048725,10293314,华为P50研发进展与PS5价格泄露,"['华为', '手机', '处理器', '游戏主机', '操作系统']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目描述:

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:

  1. heights will have length in [1, 100] and contain integers in [0, 99].
  2. V will be in range [0, 2000].
  3. 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;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值