-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrapping-rain-water.cpp
42 lines (37 loc) · 1.2 KB
/
trapping-rain-water.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
/*
https://leetcode.com/problems/trapping-rain-water/submissions/1094309791?envType=study-plan-v2&envId=top-interview-150
Runtime Details 7ms
Beats 93.12%of users with C++
Memory Details 20.02MB
Beats 74.27%of users with C++
*/
class Solution {
public:
int trap(vector<int> &height) {
int cur_left = 0, max_left = 0, total_water = 0;
int cur_right = height.size() - 1;
int max_right = cur_right;
// checking from both sides
while (cur_left < cur_right) {
if (height[cur_left] <
height[cur_right]) // the left in the optimal solution
{
if (height[max_left] <= height[cur_left]) {
max_left = cur_left;
} else {
total_water += height[max_left] - height[cur_left];
}
cur_left++;
} else {
if (height[max_right] <= height[cur_right]) {
max_right = cur_right;
} else {
total_water += height[max_right] - height[cur_right];
}
cur_right--;
}
}
return total_water;
}
int total_water_;
};