-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathsolution.cpp
48 lines (45 loc) · 1.25 KB
/
solution.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
43
44
45
46
47
48
/**
* 320 / 320 test cases passed.
* Runtime: 4 ms
* Memory Usage: 14.1 MB
*/
class Solution {
public:
int trap(vector<int>& height) {
int len = height.size();
if (len <= 2) return 0;
vector<int> leftHeight(len, *height.begin());
vector<int> rightHeight(len, height.back());
for (int i = 1; i < len; ++ i) {
leftHeight[i] = max(leftHeight[i - 1], height[i]);
rightHeight[len - i - 1] = max(rightHeight[len - i], height[len - i - 1]);
}
int ans = 0;
for (int i = 1; i < len - 1; ++ i) {
ans += min(leftHeight[i], rightHeight[i]) - height[i];
}
return ans;
}
};
/**
* 320 / 320 test cases passed.
* Runtime: 12 ms
* Memory Usage: 13.7 MB
*/
class Solution2 {
public:
int trap(vector<int>& height) {
int len = height.size();
if (len <= 2) return 0;
int ans = 0;
int black = accumulate(height.begin(), height.end(), 0);
int area = 0;
for (int l = 0, r = len - 1, hi = 1; l <= r;) {
while (l <= r && height[l] < hi) ++ l;
while (l <= r && height[r] < hi) -- r;
++ hi;
area += r - l + 1;
}
return area - black;
}
};