[Leetcode] 755. Pour Water 解题报告

这篇博客详细介绍了LeetCode上的第755题——Pour Water的问题。文章讨论了如何处理地形高度数据,当水滴在特定位置落下后,水会如何在地形上分布。解题策略包括如何确定水的流动方向和如何在数组中模拟水的积累过程。

摘要生成于 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 Vunits 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]
    Explanation:
    #       #
    #       #
    ##  # ###
    #########
     0123456    <- index
    
    The first drop of water lands at index K = 3:
    
    #       #
    #   w   #
    ##  # ###
    #########
     0123456    
    
    When moving left or right, the water can only move to the same level or a lower level.
    (By level, we mean the total height of the terrain plus any water in that column.)
    Since moving left will eventually make it fall, it moves left.
    (A droplet "made to fall" means go to a lower height than it was at previously.)
    
    #       #
    #       #
    ## w# ###
    #########
     0123456    
    
    Since moving left will not make it fall, it stays in place.  The next droplet falls:
    
    #       #
    #   w   #
    ## w# ###
    #########
     0123456  
    
    Since the new droplet moving left will eventually make it fall, it moves left.
    Notice that the droplet still preferred to move left,
    even though it could move right (and moving right makes it fall quicker.)
    
    #       #
    #  w    #
    ## w# ###
    #########
     0123456  
    
    #       #
    #       #
    ##ww# ###
    #########
     0123456  
    
    After those steps, the third droplet falls.
    Since moving left would not eventually make it fall, it tries to move right.
    Since moving right would eventually make it fall, it moves right.
    
    #       #
    #   w   #
    ##ww# ###
    #########
     0123456  
    
    #       #
    #       #
    ##ww#w###
    #########
     0123456  
    
    Finally, the fourth droplet falls.
    Since moving left would not eventually make it fall, it tries to move right.
    Since moving right would not eventually make it fall, it stays in place:
    
    #       #
    #   w   #
    ##ww#w###
    #########
     0123456  
    
    The final answer is [2,2,2,3,2,2,2]:
    
        #    
     ####### 
     ####### 
     0123456 
    

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

    思路

    我们在第K个位置上滴下一滴水,然后计算这滴水最终会流向左边的某个slot,还是右边的某个slot,还是保留在原地。这样当V滴水都滴下时,我们就得到了最终的结果。在下面的pourLeft函数中,判断水是否还会继续向左流有三个条件,分别说明一下:

    1)T >= 0:T不能越界;

    2)heights[T] + waters[T] < heights[K] + waters[K]:这样可以保证K位置高于T位置,所以水才可以从K流动到T(否则就保留在了K这个slot上面了);

    3)heights[T] + waters[T] <= heights[T + 1] + waters[T + 1]:这样保证水可以顺利的从T+1位置流向T位置,否则即使T左边还有更低的位置,水也无法越过T位置。

    当然pourRight函数中的判断条件完全类似。

    代码

    class Solution {
    public:
        vector<int> pourWater(vector<int>& heights, int V, int K) {
            vector<int> waters(heights.size(), 0);
            int max_diff = 1;
            while (V > 0) {         // try to spread water left and right
                ++waters[K];
                --V;
                int left = pourLeft(heights, waters, K, max_diff);
                if (left != -1) {   // we can pour left
                    ++waters[left], --waters[K];
                }
                else {              // if cannot pour left, we try pouring right
                    max_diff = 1;
                    int right = pourRight(heights, waters, K, max_diff);
                    if (right != -1) {
                        ++waters[right], --waters[K];
                    }
                }
                max_diff = 1;
            }
            for (int i = 0; i < heights.size(); ++i) {
                waters[i] += heights[i];
            }
            return waters;
        }
    private:
        int pourLeft(vector<int> &heights, vector<int> &waters, int K, int &max_diff) {
            int T = K - 1, max_T = -1;
            while (T >= 0 && heights[T] + waters[T] < heights[K] + waters[K] &&
                   heights[T] + waters[T] <= heights[T + 1] + waters[T + 1]) {
                if (heights[K] + waters[K] - heights[T] - waters[T] > max_diff) {
                    max_T = T;
                    max_diff = heights[K] + waters[K] - heights[T] - waters[T];
                }
                --T;
            }
            return max_T;
        }
        int pourRight(vector<int> &heights, vector<int> &waters, int K, int &max_diff) {
            int T = K + 1, max_T = -1;
            while (T < heights.size() && heights[T] + waters[T] < heights[K] + waters[K] &&
                   heights[T] + waters[T] <= heights[T - 1] + waters[T - 1]) {
                if (heights[K] + waters[K] - heights[T] - waters[T] > max_diff) {
                    max_T = T;
                    max_diff = heights[K] + waters[K] - heights[T] - waters[T];
                }
                ++T;
            }
            return max_T;
        }
    };

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值