forked from zhuli19901106/lintcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuilding-outline(AC).cpp
More file actions
94 lines (88 loc) · 2.27 KB
/
building-outline(AC).cpp
File metadata and controls
94 lines (88 loc) · 2.27 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#include <algorithm>
#include <map>
using namespace std;
struct Term {
int x, y;
int op;
bool operator < (const Term &t) const {
if (x != t.x) {
return x < t.x;
}
if (op != t.op) {
return op < t.op;
}
if (op == 0) {
// Start of a building
return y > t.y;
} else {
// End of a building
return y < t.y;
}
}
};
class Solution {
public:
/**
* @param buildings: A list of lists of integers
* @return: Find the outline of those buildings
*/
vector<vector<int> > buildingOutline(vector<vector<int> > &buildings) {
vector<vector<int> > &a = buildings;
vector<Term> v;
Term t;
int n = a.size();
int i;
for (i = 0; i < n; ++i) {
t.x = a[i][0];
t.y = a[i][2];
t.op = 0;
v.push_back(t);
t.x = a[i][1];
t.y = a[i][2];
t.op = 1;
v.push_back(t);
}
sort(v.begin(), v.end());
map<int, int> m;
vector<int> px, py;
bool b;
m[0] = 1;
n = v.size();
for (i = 0; i < n; ++i) {
if (v[i].op == 0) {
// Start of a building
if (m.rbegin()->first < v[i].y) {
px.push_back(v[i].x);
py.push_back(v[i].y);
}
++m[v[i].y];
} else {
// End of a building
b = false;
--m[v[i].y];
if (m[v[i].y] == 0) {
b = v[i].y == m.rbegin()->first;
m.erase(v[i].y);
}
if (b) {
px.push_back(v[i].x);
py.push_back(m.rbegin()->first);
}
}
}
n = px.size();
vector<vector<int> > ans;
vector<int> seg(3);
for (i = 0; i < n - 1; ++i) {
// Ignore all zeros
if (py[i] == 0) {
continue;
}
seg[0] = px[i];
seg[1] = px[i + 1];
seg[2] = py[i];
ans.push_back(seg);
}
return ans;
}
};