The Skyline Problem

来源:互联网 发布:快捷矩阵官网 编辑:程序博客网 时间:2024/05/04 22:28
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;            }        });        queue.offer(0);        int prev = 0;        for (int[] h: heights) {            if (h[1] < 0) {                queue.offer(-h[1]);            } else {                queue.remove(h[1]);            }            int cur = queue.peek();            if (cur != prev) {                res.add(new int[]{h[0], cur});                prev = cur;            }        }        return res;    }}

0 0