StoneWall

problem

solution

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
/// you can use includes, for example:
// #include <algorithm>

// you can write to stdout for debugging purposes, e.g.
// cout << "this is a debug message" << endl;
#include<stack>
int solution(vector<int> &H) {
// write your code in C++14 (g++ 6.2.0)

// monotonic stack
stack<int> sta;
int n = H.size(), block = 0;
// 8 8 5 7 9 8 7 4 8
//
for(int i=0;i<n;++i){
while(!sta.empty() && sta.top() > H[i] ){

sta.pop();
}
if(sta.empty() || sta.top() < H[i] ) {
sta.push(H[i]);
block++;
}
}
return block ;
}

analysis

  • time complexity O(n)
  • space complexity O(n)