-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path42.cpp
More file actions
25 lines (22 loc) · 679 Bytes
/
42.cpp
File metadata and controls
25 lines (22 loc) · 679 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
class Solution {
public:
int trap(vector<int>& height) {
int maxheightL[height.size()];
int maxheightR[height.size()];
int max = 0;
for(int i = 0; i < height.size(); i++){
if(height[i] > max) max = height[i];
maxheightL[i] = max;
}
max = 0;
for(int i = height.size()-1; i >= 0; i--){
if(height[i] > max) max = height[i];
maxheightR[i] = max;
}
int area = 0;
for(int i = 0; i < height.size(); i++){
area += std::max(std::min(maxheightR[i], maxheightL[i])-height[i],0);
}
return area;
}
};