Leetcode42. 接雨水
讲的好的视频讲解
【很难想象这up刷题的精神状态 Leetcode42. 接雨水】
https://www.bilibili.com/video/BV1MC411n7Af/?share_source=copy_web&vd_source=afbacdc02063c57e7a2ef256a4db9d2a
rm是right max的意思,lm是left max的意思
时间复杂度:
O
(
n
)
O(n)
O(n)
空间复杂度:
S
(
1
)
S(1)
S(1)
class Solution {
public:
int trap(vector<int>& height) {
int res = 0 , l = 0,r = height.size() - 1 ,lm = 0 ,rm = 0;
while(l < r){
lm = max(lm,height[l]);
rm = max(rm , height[r]);
if(lm < rm)
res += lm - height[l++];//柱子宽度是1,面积计算过程省略了*1的步骤
else
res += rm - height[r--];
}
return res;
}
};