-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path42.cpp
More file actions
executable file
·29 lines (29 loc) · 976 Bytes
/
42.cpp
File metadata and controls
executable file
·29 lines (29 loc) · 976 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
#include <vector>
#include <iostream>
using namespace std;
class Solution {
public:
int trap(vector<int>& height) {
if (height.size() == 0)
return 0;
int left_wall = 0;
int answer = 0;
while (left_wall < height.size()-1) {
int right_wall = left_wall + 1;
int baseline = 0;
while (right_wall < height.size() && height[right_wall] < height[left_wall]){
if (height[right_wall] > baseline) baseline = height[right_wall];
right_wall++;
}
if (right_wall != height.size() && height[right_wall] >= height[left_wall]){
answer += (height[right_wall]-baseline) * (right_wall - left_wall-1);
//cout<<left_wall<<" "<<right_wall<<" "<<1 * (right_wall - left_wall-1)<<endl;
left_wall++;
}else
{
left_wall++;
}
}
return answer;
}
};