Skip to content

Commit

Permalink
이슈 #453에서 솔루션 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
actions-user committed Feb 23, 2025
1 parent 8dab5e2 commit 12391f3
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions LeetCode/Trapping_Rain_Water.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Solution {
public:
int trap(vector<int>& 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;
}
};

0 comments on commit 12391f3

Please sign in to comment.