title | location | images | tags | privacy | createdAt | ||
---|---|---|---|---|---|---|---|
Untitled Post |
19.2053, 72.9481 |
|
public |
2025-10-09 16:06:52 UTC |
https://leetcode.com/problems/trapping-rain-water/ Method-1 two pointer
class Solution {
public:
int trap(vector<int>& height) {
int leftmax=0,rightmax=0,l=0,r=height.size()-1;
int total=0;
while(l<r){
if(height[l]<=height[r]){
if(leftmax>height[l]){
total += leftmax -height[l];
}else{
leftmax = max(leftmax,height[l]);
}
l++;
}
else{
if(rightmax>height[r]){
total += rightmax -height[r];
}else {
rightmax = max(rightmax,height[r]);
}
r--;
}
}
return total;
}
};
method-2 ; prifix and suffix max: