forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.cpp
36 lines (33 loc) · 1.04 KB
/
Solution.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
class Solution {
public:
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
set<int> poss;
map<int, int> m;
for (auto v : buildings) {
poss.insert(v[0]);
poss.insert(v[1]);
}
int i = 0;
for (int pos : poss)
m.insert(pair<int, int>(pos, i++));
vector<int> highs(m.size(), 0);
for (auto v : buildings) {
const int b = m[v[0]], e = m[v[1]];
for (int i = b; i < e; ++i)
highs[i] = max(highs[i], v[2]);
}
vector<pair<int, int>> res;
vector<int> mm(poss.begin(), poss.end());
for (int i = 0; i < highs.size(); ++i) {
if (highs[i] != highs[i + 1])
res.push_back(pair<int, int>(mm[i], highs[i]));
else {
const int start = i;
res.push_back(pair<int, int>(mm[start], highs[i]));
while (highs[i] == highs[i + 1])
++i;
}
}
return res;
}
};