-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrapping_water.cpp
More file actions
49 lines (46 loc) · 1.46 KB
/
trapping_water.cpp
File metadata and controls
49 lines (46 loc) · 1.46 KB
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
49
int trap(int A[], int n) {
if (n<=2) return 0;
int sum = 0;
stack<pair<int,int>> s;
int i=0;
while(A[i]==0){
i++;
}
for( ; i<n; i++) {
if(i+1<n && A[i]<=A[i+1]) continue;
else break;
}
if(i<n) {
s.push(make_pair(A[i], i)); i++;
}else {
return 0;
}
int bar = 0;
for( ; i<n; i++) {
if(s.empty()) {
return 0;
}
if(A[i]<s.top().first) {
s.push(make_pair(A[i], i));
}else if(A[i]==s.top().first){
s.top().second = i;
}else{
bar = s.top().first; s.pop();
int height = (A[i] > s.top().first) ? s.top().first-bar : A[i]-bar;
int len = i - s.top().second -1;
sum += height*len;
while(!s.empty()&& A[i]>=s.top().first) {
bar = s.top().first; s.pop();
if(s.empty()) {
s.push(make_pair(A[i], i)); break;
}else {
int height = (A[i] > s.top().first) ? s.top().first-bar : A[i]-bar;
int len = i-s.top().second -1;
sum += height*len;
}
}
s.push(make_pair(A[i], i));
}
}
return sum;
}