🎉 One-stop destination for all your technical interview Preparation 🎉
Given n non-negative integers representing an elevation map where the width of each bar is 1
, compute how much water it can trap after raining.
min(maxLeft(i),maxRight(i))-a[i]
class Solution {
public:
int trap(vector<int>& height)
{
int n = height.size();
int left = 0;
int right = n - 1;
int res = 0;
int maxleft = 0, maxright = 0;
while (left <= right) {
if (height[left] <= height[right]) { // update max left
if (height[left] >= maxleft) maxleft = height[left];
else res += maxleft - height[left];
left++;
} else { // update max right
if (height[right] >= maxright) maxright = height[right];
else res += maxright - height[right];
right--;
}
}
return res;
}
};