-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththe-skyline-problem.cpp
84 lines (67 loc) · 1.47 KB
/
the-skyline-problem.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
* Copyright (c) 2021, Christopher Friedt
*
* SPDX-License-Identifier: MIT
*/
// clang-format off
// name: the-skyline-problem
// url: https://leetcode.com/problems/the-skyline-problem
// difficulty: 3
// clang-format on
#include <vector>
using namespace std;
enum {
LEFT,
RIGHT,
HEIGHT,
};
enum {
X,
Y,
};
template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
os << "[";
for (int i = 0, N = v.size(); i < N; ++i) {
os << v[i];
if (i < N - 1) {
os << ",";
}
}
os << "]";
return os;
}
class Solution {
public:
vector<vector<int>> getSkyline(vector<vector<int>> &B) {
vector<vector<int>> S;
int xmin = B[0][LEFT];
int xmax = xmin;
for (auto &b : B) {
xmax = max(xmax, b[RIGHT]);
}
vector<int> h(xmax - xmin + 1, 0);
for (auto &b : B) {
for (auto x = b[LEFT]; x <= b[RIGHT]; ++x) {
h[x - xmin] = max(h[x - xmin], b[HEIGHT]);
}
}
// cout << "h: " << h << endl;
S.push_back(vector<int>{xmin, h[0]});
for (int x = xmin + 1; x <= xmax; ++x) {
auto &h_curr = h[x - xmin];
auto &h_prev = h[x - xmin - 1];
if (h_curr == h_prev) {
continue;
}
if (h_curr > h_prev) {
S.push_back(vector<int>{x, h_curr});
} else {
S.push_back(vector<int>{x - 1, h_curr});
}
// cout << "S: " << S << endl;
}
S.push_back(vector<int>{xmax, 0});
// cout << "S: " << S << endl;
return S;
}
};