Skip to content

Commit 799def9

Browse files
authored
区间离散化解法(低效 120ms)
1 parent 6f67f4c commit 799def9

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed

solution/0218.The Skyline Problem

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// 区间离散化解法(低效 120ms)
2+
class Solution {
3+
public:
4+
vector<pair<int, int>> getSkyline(vector<vector<int>>& buildings) {
5+
set<int> poss ;
6+
map<int, int> m ;
7+
for (auto v: buildings)
8+
{
9+
poss.insert(v[0]) ;
10+
poss.insert(v[1]) ;
11+
}
12+
13+
int i = 0 ;
14+
for (int pos: poss)
15+
m.insert(pair<int, int>(pos, i++)) ;
16+
17+
vector<int> highs(m.size(), 0) ;
18+
for (auto v: buildings)
19+
{
20+
const int b = m[v[0]], e = m[v[1]] ;
21+
for (int i = b; i < e; ++i)
22+
highs[i] = max(highs[i], v[2]) ;
23+
}
24+
25+
vector<pair<int, int>> res ;
26+
vector<int> mm(poss.begin(), poss.end()) ;
27+
for (int i = 0; i < highs.size(); ++i)
28+
{
29+
if (highs[i] != highs[i+1])
30+
res.push_back(pair<int, int>(mm[i], highs[i])) ;
31+
else
32+
{
33+
const int start = i ;
34+
res.push_back(pair<int, int>(mm[start], highs[i])) ;
35+
while (highs[i] == highs[i+1])
36+
++i ;
37+
}
38+
}
39+
return res ;
40+
}
41+
};

0 commit comments

Comments
 (0)