-
-
Notifications
You must be signed in to change notification settings - Fork 9k
/
Copy pathSolution.cpp
41 lines (38 loc) · 1.15 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
37
38
39
40
41
// 区间离散化解法(低效 120ms)
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 ;
}
};