-
Notifications
You must be signed in to change notification settings - Fork 25
/
StoneWall.cpp
57 lines (51 loc) · 1.92 KB
/
StoneWall.cpp
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
50
51
52
53
54
55
56
57
// https://app.codility.com/programmers/lessons/7-stacks_and_queues/stone_wall/
//
// Task Score: 100%
// Correctness: 100%
// Performance: 100%
// Detected time complexity: O(N)
//
#include <vector>
#include <stack>
using namespace std;
int solution(vector<int> &H) {
int N = H.size();
// Algorithm seems to be:
// 1. Every time building grows, push a height
// 2. If height stays same, do nothing
// 3. When height drops, pop while queue not empty and last height greater than current height
// 4. If height is now equal to last height, then continue along, otherwise push new height
// 5. At end need to close out all current blocks in queue
// Should be 0(N) time complexity
std::stack<int> heights;
int blocks = 0;
for (int i=0; i<N; i++) {
if ( !heights.size() ) {
// stack is empty, either because we just started
// or we ended a current building line. So just push it
heights.push(H[i]);
} else if ( H[i] == heights.top() ) {
} else if ( H[i] > heights.top() ) {
// height grew, push to stack
heights.push(H[i]);
} else {
// height must have dropped. Pop height and increment bricks
// while the last height is taller than this height (and stack not empty)
while ( heights.size() && (heights.top() > H[i]) ) {
blocks++;
heights.pop();
}
// If stack is empty, or H[i] not equal heights.top(), need to push this height onto the stack
// If the heights are equal, then continue along (since it wasn't popped)
if ( !heights.size() || (heights.top() != H[i]) ) {
heights.push(H[i]);
}
}
}
// At the very end, need to close out all blocks in queue
while ( heights.size() ) {
blocks++;
heights.pop();
}
return blocks;
}