diff --git a/Arrays/TrappingRainwater.cpp b/Arrays/TrappingRainwater.cpp new file mode 100644 index 0000000..3e37f16 --- /dev/null +++ b/Arrays/TrappingRainwater.cpp @@ -0,0 +1,28 @@ +class Solution { +public: + int trap(vector& height) { + int n = height.size(); + int left = 0, right = n - 1; + int maxleft = 0, maxright = 0; + int res = 0; + + while(left <= right){ + + if(height[left] <= height[right]){ + if(height[left] >= maxleft) maxleft = height[left]; + else + res+= maxleft - height[left]; + + left++; + } + else{ + if(height[right] >= maxright) maxright = height[right]; + else + res+= maxright - height[right]; + + right--; + } + } + return res; + } +}; \ No newline at end of file