-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathTheSkylineProblem.java
More file actions
50 lines (46 loc) · 1.74 KB
/
TheSkylineProblem.java
File metadata and controls
50 lines (46 loc) · 1.74 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
class Edge implements Comparable<Edge> {
public int pos;
public int height;
public boolean isStart;
public Edge(int pos, int height, boolean isStart) {
this.pos = pos;
this.height = height;
this.isStart = isStart;
}
public int compareTo(Edge e) {
if (this.pos != e.pos) return Integer.compare(this.pos, e.pos);
if (this.isStart && e.isStart) return Integer.compare(e.height, this.height);
if (!this.isStart && !e.isStart) return Integer.compare(this.height, e.height);
return this.isStart ? -1 : 1;
}
}
public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new ArrayList<>();
if (buildings == null || buildings.length == 0
|| buildings[0].length == 0) return res;
List<Edge> edges = new ArrayList<>();
for (int[] building : buildings) {
Edge start = new Edge(building[0], building[2], true);
Edge end = new Edge(building[1], building[2], false);
edges.add(start);
edges.add(end);
}
Collections.sort(edges);
PriorityQueue<Integer> heap = new PriorityQueue<>(10, Collections.reverseOrder());
for (Edge edge : edges) {
if (edge.isStart) {
if (heap.isEmpty() || edge.height > heap.peek()) {
res.add(new int[] {edge.pos, edge.height});
}
heap.add(edge.height);
} else {
heap.remove(edge.height);
if (heap.isEmpty() || edge.height > heap.peek()) {
res.add(new int[] {edge.pos, heap.isEmpty() ? 0 : heap.peek()});
}
}
}
return res;
}
}