public class Solution {
public List<int[]> getSkyline(int[][] buildings) {
List<int[]> res = new LinkedList<>();
List<int[]> heights = new LinkedList<>();
for (int[] b: buildings) {
heights.add(new int[]{b[0], -b[2]});
heights.add(new int[]{b[1], b[2]});
}
Collections.sort(heights, new Comparator<int[]>(){
@Override
public int compare(int[] a, int[] b) {
if (a[0] != b[0]) {
return a[0] - b[0];
} else {
return a[1] - b[1];
}
}
});
PriorityQueue<Integer> queue = new PriorityQueue<>(11, new Comparator<Integer>(){
@Override
public int compare(Integer a, Integer b){
return b - a;
}
});
int prev = 0;
queue.offer(0);
for (int[] b: heights) {
if (b[1] < 0) {
queue.offer(-b[1]);
} else {
queue.remove(b[1]);
}
int cur = queue.peek();
if (prev != cur) {
res.add(new int[]{b[0], cur});
prev = cur;
}
}
return res;
}
}
The Skyline Problem
最新推荐文章于 2021-07-13 01:17:21 发布