Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit de52797

Browse files
区间离散化解法(低效 120ms)
未使用 线段树
1 parent ebb4eea commit de52797

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
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)

AltStyle によって変換されたページ (->オリジナル) /