首页 > 其他分享 >LeetCode42-接雨水

LeetCode42-接雨水

时间:2022-09-26 16:47:30浏览次数:35  
标签:int res len height 雨水 LeetCode42

int trap(vector<int>& height) {
    int res = 0;
    int len = (int)height.size();
    if (len <= 1) {
        return res;
    }
    int l = 0;
    int r = len - 1;
    int l_height = 0;
    int r_height = 0;
    while (l < r) {
        if (height[l] < height[r]) {
            l_height = max(l_height, height[l]);
            res += l_height - height[l];
            l++;
        } else {
            r_height = max(r_height, height[r]);
            res += r_height - height[r];
            r--;
        }
    }
    return res;
}

标签:int,res,len,height,雨水,LeetCode42
From: https://www.cnblogs.com/txtp/p/16731431.html

相关文章

  • BM93,BM94接雨水问题(最大水量maxArea和总水量trapWater问题)(双指针)
    总水量问题BM94trapWater()描述给定一个整形数组arr,已知其中所有的值都是非负的,将这个数组看作一个柱子高度图,计算按此排列的柱子,下雨之后能接多少雨水。(数组以外的......
  • 题解:「GLR-R3」雨水
    题解:「GLR-R3」雨水题目链接前言先吐槽一下,这个英文是真的坑。constintMAXN=712;//Setarightvalueaccordingtoyoursolution.为啥不能直接把数组下标设为......
  • leetcode42-接雨水
    接雨水dp维护两个dp数组,分别记录左侧和右侧的最大值。当前位置的雨水就是左右两侧最大值的较小值减去当前位置的高度,将所有雨水累加即可得到结果。classSolution{......