From 12391f3c36139673a0e6a187c934ae71eb336116 Mon Sep 17 00:00:00 2001 From: GitHub Action Date: Sun, 23 Feb 2025 01:37:46 +0000 Subject: [PATCH] =?UTF-8?q?=EC=9D=B4=EC=8A=88=20#453=EC=97=90=EC=84=9C=20?= =?UTF-8?q?=EC=86=94=EB=A3=A8=EC=85=98=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- LeetCode/Trapping_Rain_Water.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 LeetCode/Trapping_Rain_Water.cpp 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