-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
sunset_view.cc
44 lines (38 loc) · 1.32 KB
/
sunset_view.cc
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
#include <iterator>
#include <stack>
#include <vector>
#include "test_framework/generic_test.h"
using std::stack;
using std::vector;
vector<int> ExamineBuildingsWithSunset(
vector<int>::const_iterator sequence_begin,
const vector<int>::const_iterator& sequence_end) {
int building_idx = 0;
struct BuildingWithHeight {
int id, height;
};
stack<BuildingWithHeight> candidates;
while (sequence_begin != sequence_end) {
int building_height = *sequence_begin++;
while (!empty(candidates) && building_height >= candidates.top().height) {
candidates.pop();
}
candidates.emplace(BuildingWithHeight{building_idx++, building_height});
}
vector<int> buildings_with_sunset;
while (!empty(candidates)) {
buildings_with_sunset.emplace_back(candidates.top().id);
candidates.pop();
}
return buildings_with_sunset;
}
vector<int> ExamineBuildingsWithSunsetWrapper(const vector<int>& sequence) {
return ExamineBuildingsWithSunset(cbegin(sequence), cend(sequence));
}
int main(int argc, char* argv[]) {
std::vector<std::string> args{argv + 1, argv + argc};
std::vector<std::string> param_names{"sequence"};
return GenericTestMain(args, "sunset_view.cc", "sunset_view.tsv",
&ExamineBuildingsWithSunsetWrapper,
DefaultComparator{}, param_names);
}