-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.接雨水.cpp
More file actions
29 lines (27 loc) · 708 Bytes
/
42.接雨水.cpp
File metadata and controls
29 lines (27 loc) · 708 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/*
* @lc app=leetcode.cn id=42 lang=cpp
*
* [42] 接雨水
*/
// @lc code=start
class Solution {
public:
int trap(vector<int>& height)
{
int left = 0, right = height.size() - 1;
int ans = 0;
int left_max = 0, right_max = 0;
while (left < right) {
if (height[left] < height[right]) {
height[left] >= left_max ? (left_max = height[left]) : ans += (left_max - height[left]);
++left;
}
else {
height[right] >= right_max ? (right_max = height[right]) : ans += (right_max - height[right]);
--right;
}
}
return ans;
}
};
// @lc code=end