forked from JohnJordan0098/Data-Structures-Algos
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeetcode_problem.c
More file actions
42 lines (40 loc) · 924 Bytes
/
Leetcode_problem.c
File metadata and controls
42 lines (40 loc) · 924 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Solution
{
public:
int trap(vector<int> &height)
{
int len = height.size();
int temp = 0;
int res = 0;
int curr_max = 0;
for (int i = 1; i < len; i++)
{
if (height[i] >= height[curr_max])
{
res += (i - curr_max - 1) * height[curr_max] - temp;
curr_max = i;
temp = 0;
}
else
{
temp += height[i];
}
}
temp = 0;
curr_max = len - 1;
for (int i = len - 2; i >= 0; i--)
{
if (height[i] > height[curr_max])
{
res += (curr_max - i - 1) * height[curr_max] - temp;
curr_max = i;
temp = 0;
}
else
{
temp += height[i];
}
}
return res;
}
};