-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path07_4.cpp
36 lines (32 loc) · 923 Bytes
/
07_4.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
// 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;
int solution(vector<int> &H) {
int N = H.size();
int blocks = 0;
int queueSum = H[0];
vector<int> queue(1, queueSum);
for (int i = 1; i<N; i++) {
if(H[i] > H[i-1]) {
queue.push_back(H[i]-H[i-1]);
queueSum += H[i]-H[i-1];
} else {
while(H[i] < queueSum) {
queueSum -= queue.back();
queue.pop_back();
blocks++;
}
if(H[i] != queueSum) {
queue.push_back(H[i]-queueSum);
queueSum += H[i]-queueSum;
}
}
}
while(queueSum != 0) {
queueSum -= queue.back();
queue.pop_back();
blocks++;
}
return blocks;
}