Problem 218: The Skyline Problem
思路
复杂度
public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> rst = new ArrayList<int[]>();
List<int[]> height = new ArrayList<int[]>();
// store in height and sort buildings
for (int[] b : buildings) {
height.add(new int[] {b[0], -b[2]});
height.add(new int[] {b[1], b[2]});
}
Collections.sort(height, (h1, h2) -> {
if (h1[0] != h2[0]) {
return h1[0] - h2[0];
}
return h1[1] - h2[1];
});
Queue<Integer> pq = new PriorityQueue<Integer>((i1, i2) -> (i2 - i1));
int prev = 0;
pq.offer(0);
// traverse height
for (int[] h : height) {
if (h[1] < 0) {
pq.offer(-h[1]);
} else {
pq.remove(h[1]);
}
int curr = pq.peek();
if (curr != prev) {
rst.add(new int[] {h[0], curr});
prev = curr;
}
}
return rst;
}
}易错点
Last updated