Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions Solutions/42TrappingRainWater.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class Solution {
public:
int trap(vector<int>& height) {
int n = height.size();
int lmax = height[0];
int rmax = height[n-1];
int lpos = 1;
int rpos = n-2;
int water = 0;
while(lpos <= rpos)
{
if(height[lpos] >= lmax)
{
lmax = height[lpos];
lpos++;
}
else if(height[rpos] >= rmax)
{
rmax = height[rpos];
rpos--;
}
else if(lmax <= rmax && height[lpos] < lmax)
{
water += lmax - height[lpos];
lpos++;
}
else
{
water += rmax - height[rpos];
rpos--;
}

}
return water;
}
};