-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetSkyLine.h
68 lines (63 loc) · 1.74 KB
/
getSkyLine.h
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
//
// Created by so_go on 2019/6/23.
//
#ifndef SRC_GETSKYLINE_H
#define SRC_GETSKYLINE_H
#include<vector>
#include<set>
#include<functional>
#include<algorithm>
#include<iostream>
#include"printVector.h"
using namespace std;
struct Record{
int pos, height;
friend ostream & operator<<(ostream & os, const Record & r){
os << r.pos << ':' << r.height;
return os;
}
bool operator<(const Record & b) const{
return pos < b.pos;
}
};
class GetSkyLine {
public:
vector<vector<int>> getSkyline(vector<vector<int>>& buildings) {
multiset<int> s{0, };
vector<Record> vec;
vector<vector<int>> res;
for(int i = 0; i < buildings.size(); i++){
// left height
vec.push_back(Record{buildings[i][0], buildings[i][2]});
// right height
vec.push_back(Record{buildings[i][1], -buildings[i][2]});
}
sort(vec.begin(), vec.end());
printVector(vec);
int lastPos, oldMax;
auto ptr = vec.begin();
while(ptr != vec.end()){
oldMax = *s.rbegin();
do{
if(ptr->height > 0){
s.insert(ptr->height);
}
else{
s.erase(s.find(-ptr->height));
}
lastPos = ptr->pos;
ptr++;
}
while(ptr != vec.end() and ptr->pos == lastPos);
if(*s.rbegin() != oldMax){
res.push_back({lastPos, *s.rbegin()});
}
// for(auto stPtr = s.begin(); stPtr != s.end(); stPtr++){
// cout << *stPtr << ' ';
// }
// cout << endl;
}
return res;
}
};
#endif //SRC_GETSKYLINE_H