leetcdoe 42. 接雨水 (双指针 单调栈)
链接:https://leetcode-cn.com/problems/trapping-rain-water/
题目
给定 n 个非负整数表示每个宽度为 1 的柱子的高度图,计算按此排列的柱子,下雨之后能接多少雨水。
用例
示例 1:

输入:height = [0,1,0,2,1,0,1,3,2,1,2,1]
输出:6
解释:上面是由数组 [0,1,0,2,1,0,1,3,2,1,2,1] 表示的高度图,在这种情况下,可以接 6 个单位的雨水(蓝色部分表示雨水)。
示例 2:
输入:height = [4,2,0,3,2,5]
输出:9
提示:
n == height.length
0 <= n <= 3 * 104
0 <= height[i] <= 105
思路
通过双指针来解决接雨水问题,一个指针指向左侧边界,另一指针指向右侧边界,并设置两个值分别记录左侧和右侧高度的最大值。判断左右最高点值哪个小,在靠近小高度的那侧作积水计算
class Solution {
public:
int trap(vector<int>& height) {
int n=height.size();
int left=0,right=n-1;
int leftmax=0,rightmax=0;
int sum=0;
while(right>left)
{
leftmax=max(leftmax,height[left]);
rightmax=max(rightmax,height[right]);
if(leftmax>rightmax)
{
sum+=rightmax-height[right];
right--;
}else
{
sum+=leftmax-height[left];
left++;
}
}
return sum;
}
};
使用单调栈也可以实现
使用单调栈
单调递减栈
理解题目注意题目的性质,当后面的柱子高度比前面的低时,是无法接雨水的
当找到一根比前面高的柱子,就可以计算接到的雨水
所以使用单调递减栈
对更低的柱子入栈
更低的柱子以为这后面如果能找到高柱子,这里就能接到雨水,所以入栈把它保存起来
平地相当于高度 0 的柱子,没有什么特别影响
当出现高于栈顶的柱子时
41. 说明可以对前面的柱子结算了
42. 计算已经到手的雨水,然后出栈前面更低的柱子
计算雨水的时候需要注意的是
雨水区域的右边 r 指的自然是当前索引 i
底部是栈顶 st.top() ,因为遇到了更高的右边,所以它即将出栈,使用 cur 来记录它,并让它出栈
左边 l 就是新的栈顶 st.top()
雨水的区域全部确定了,水坑的高度就是左右两边更低的一边减去底部,宽度是在左右中间
使用乘法即可计算面积
class Solution {
public:
int trap(vector<int>& height) {
int n=height.size();
int left=0,right=n-1;
int leftmax=0,rightmax=0;
int sum=0;
while(right>left)
{
leftmax=max(leftmax,height[left]);
rightmax=max(rightmax,height[right]);
if(leftmax>rightmax)
{
sum+=rightmax-height[right];
right--;
}else
{
sum+=leftmax-height[left];
left++;
}
}
return sum;
}
};

浙公网安备 33010602011771号