[LeetCode]The Skyline Problem
public class Solution { public List<int[]> getSkyline(int[][] buildings) { List<int[]> height = new ArrayList<int[]>(); List<int[]> result = new ArrayList<int[]>(); for (int[] tmp : buildings) { height.add(new int[]{tmp[0], tmp[2]}); height.add(new int[]{tmp[1], -tmp[2]}); } Collections.sort(height, new Comparator<int[]>() { public int compare(int[] o1, int[] o2) { if (o1[0] != o2[0]) { return o1[0] - o2[0]; } else { return o2[1] - o1[1]; } } }); Queue<Integer> queue = new PriorityQueue<Integer>(1, new Comparator<Integer>() { public int compare(Integer o1, Integer o2) { return o2 - o1; } }); int pre = 0; queue.offer(0); for (int[] tmp : height) { if (tmp[1] > 0) { queue.offer(tmp[1]); } else { queue.remove(-tmp[1]); } int cur = queue.peek(); if (cur != pre) { result.add(new int[]{tmp[0], cur}); pre = cur; } } return result; } }