-
-
Notifications
You must be signed in to change notification settings - Fork 291
Expand file tree
/
Copy path218.java
More file actions
140 lines (131 loc) · 4.38 KB
/
218.java
File metadata and controls
140 lines (131 loc) · 4.38 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
__________________________________________________________________________________________________
sample 1 ms submission
class Solution {
private int begin = 0;
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> roofs = new ArrayList<>();
int[] floor = {0, 0};
roofs.add(floor);
for ( int[] building : buildings ) {
helpAdd(roofs, building);
}
for ( int i = 0; i < roofs.size() - 1; i++ ) {
int[] roof = roofs.get(i);
int[] next = roofs.get(i+1);
if ( roof[1] == next[1] ) {
roofs.remove(i+1);
i--;
}
}
int[] first = roofs.get(0);
if ( first[1] == 0 ) {
roofs.remove(0);
}
return roofs;
}
private void helpAdd(List<int[]> roofs, int[] bu) {
for ( int i = begin; i < roofs.size() - 1; i++ ) {
int[] roof = roofs.get(i);
int[] next = roofs.get(i+1);
if ( roof[0] >= bu[1] ) {
break;
}
if ( roof[1] >= bu[2] || next[0] <= bu[0] ) {
continue;
}
if ( roof[0] < bu[0] ) {
if ( next[0] <= bu[1] ) {
int[] nroof = {bu[0], bu[2]};
roofs.add(++i, nroof);
begin = i;
} else {
int[] nroof = {bu[0], bu[2]};
int[] nroof2 = {bu[1], roof[1]};
roofs.add(i+1, nroof2);
roofs.add(i+1, nroof);
begin = i + 1;
break;
}
} else {
if ( next[0] <= bu[1] ) {
roof[1] = bu[2];
} else {
int[] nroof = {bu[1], roof[1]};
roof[1] = bu[2];
roofs.add(i+1, nroof);
break;
}
}
}
int[] last = roofs.get(roofs.size()-1);
if ( last[1] < bu[2] && last[0] < bu[1] ) {
if ( last[0] < bu[0] ) {
int[] nroof = {bu[0], bu[2]};
int[] nroof2 = {bu[1], last[1]};
roofs.add(nroof);
roofs.add(nroof2);
begin = roofs.size() - 2;
} else {
int[] nroof = {bu[1], last[1]};
last[1] = bu[2];
roofs.add(nroof);
}
}
}
}
__________________________________________________________________________________________________
sample 36968 kb submission
class Solution {
List<List<Integer>> ans;
PriorityQueue<Node> queue;
public List<List<Integer>> getSkyline(int[][] buildings) {
queue = new PriorityQueue<Node>((a, b)->(b.h - a.h));
ans = new ArrayList<>();
for (int i=0; i<buildings.length; i++) {
int[] building = buildings[i];
Node node = new Node(building[0], building[1], building[2]);
while (!queue.isEmpty() && queue.peek().r < node.l) {
checkQueue();
}
queue.offer(node);
Node top = queue.poll();
if (ans.size() == 0 || ans.get(ans.size()-1).get(1) != top.h) {
if (ans.size() != 0 && ans.get(ans.size()-1).get(0) == top.l) {
ans.remove(ans.size()-1);
}
addPoint(top.l, top.h);
}
queue.offer(top);
}
while (!queue.isEmpty()) {
checkQueue();
}
return ans;
}
private void checkQueue() {
Node lastNode = queue.poll();
while (!queue.isEmpty() && queue.peek().r <= lastNode.r) queue.poll();
if (queue.isEmpty()) {
addPoint(lastNode.r, 0);
} else {
addPoint(lastNode.r, queue.peek().h);
}
}
private void addPoint(int r, int h) {
List<Integer> newPoint = new ArrayList<>();
newPoint.add(r);
newPoint.add(h);
ans.add(newPoint);
}
class Node {
public int l;
public int r;
public int h;
public Node(int li, int ri, int hi) {
this.l = li;
this.r = ri;
this.h = hi;
}
}
}
__________________________________________________________________________________________________