diff --git a/LeetCode/Trapping_Rain_Water.cpp b/LeetCode/Trapping_Rain_Water.cpp new file mode 100644 index 0000000..067e6a5 --- /dev/null +++ b/LeetCode/Trapping_Rain_Water.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + int trap(vector& height) { + int left = 0; + int right = height.size() - 1; + int left_max = height[left]; + int right_max = height[right]; + int water = 0; + + while (left < right) { + if (height[left] < height[right]) { + if (height[left] >= left_max) { + left_max = height[left]; + } else { + water += left_max - height[left]; + } + left++; + } else { + if (height[right] >= right_max) { + right_max = height[right]; + } else { + water += right_max - height[right]; + } + right--; + } + } + + return water; + } +}; \ No newline at end of file