【LeetCode每日一题】——面试题17.21.直方图的水量

一【题目类别】

  • 双指针

二【题目难度】

  • 困难

三【题目编号】

  • 面试题17.21.直方图的水量

四【题目描述】

  • 给定一个直方图(也称柱状图),假设有人从上面源源不断地倒水,最后直方图能存多少水量?直方图的宽度为 1。
  • 在这里插入图片描述
  • 上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的直方图,在这种情况下,可以接 6 个单位的水(蓝色部分表示水)。 感谢 Marcos 贡献此图。

五【题目示例】

  • 示例:
    • 输入: [0,1,0,2,1,0,1,3,2,1,2,1]
    • 输出: 6

六【解题思路】

  • 本题是双指针的经典应用,解题思路也有一些技巧,我们需要利用木桶的短板效应:一个木桶盛水的多少,由木桶中最短的板子决定
  • 所以我们使用leftMax记录左边最长的板子,使用rightMax记录右边最长的板子,使用left记录左边的数组下标,使用right记录右边的数组下标
  • 对于任意一个位置,只要右边有比左边高的板子,不管中间是什么情况,此位置能存的雨水量,之和最左边的板子相关
  • 对于任意一个位置,只要左边有比右边高的板子,不管中间是什么情况,此位置能存的雨水量,之和最右边的板子相关
  • 基于这个思路,遍历整个数组,不停的更新左边最高的板子和右边最高的板子,并求和每个位置可以存的雨水量
  • 最后返回结果即可

七【时间频度】

  • 时间复杂度: O ( n ) O(n) O(n),其中 n n n为传入的数组的长度
  • 空间复杂度: O ( 1 ) O(1) O(1)

八【代码实现】

  1. Java语言版
class Solution {
    
    
    public int trap(int[] height) {
    
    
        int res = 0;
        int left = 0;
        int right = height.length - 1;
        int leftMax = 0;
        int rightMax = 0;
        while(left <= right){
    
    
            if(leftMax < rightMax){
    
    
                res += Math.max(0,leftMax - height[left]);
                leftMax = Math.max(leftMax,height[left]);
                left++;
            }else{
    
    
                res += Math.max(0,rightMax - height[right]);
                rightMax = Math.max(rightMax,height[right]);
                right--;
            }
        }
        return res;
    }
}
  1. C语言版
int trap(int* height, int heightSize)
{
    
    
    int res = 0;
    int left = 0;
    int right = heightSize - 1;
    int leftMax = 0;
    int rightMax = 0;
    while(left <= right)
    {
    
    
        if(leftMax < rightMax)
        {
    
    
            res += fmax(0,leftMax - height[left]);
            leftMax = fmax(leftMax,height[left]);
            left++;
        }
        else
        {
    
    
            res += fmax(0,rightMax - height[right]);
            rightMax = fmax(rightMax,height[right]);
            right--;
        }
    }
    return res;
}
  1. Python语言版
class Solution:
    def trap(self, height: List[int]) -> int:
        res = 0
        left = 0
        right = len(height) - 1
        leftMax = 0
        rightMax = 0
        while left <= right:
            if leftMax < rightMax:
                res += max(0,leftMax-height[left])
                leftMax = max(leftMax,height[left])
                left+=1
            else:
                res += max(0,rightMax-height[right])
                rightMax = max(rightMax,height[right])
                right-=1
        return res
  1. C++语言版
class Solution {
    
    
public:
    int trap(vector<int>& height) {
    
    
        int res = 0;
        int left = 0;
        int right = height.size() - 1;
        int leftMax = 0;
        int rightMax = 0;
        while(left <= right)
        {
    
    
            if(leftMax < rightMax)
            {
    
    
                res += max(0,leftMax - height[left]);
                leftMax = max(leftMax,height[left]);
                left++;
            }
            else
            {
    
    
                res += max(0,rightMax - height[right]);
                rightMax = max(rightMax,height[right]);
                right--;
            }
        }
        return res;
    }
};

九【提交结果】

  1. Java语言版
    在这里插入图片描述

  2. C语言版
    在这里插入图片描述

  3. Python语言版
    在这里插入图片描述

  4. C++语言版
    在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/IronmanJay/article/details/129682579