码迷,mamicode.com
首页 > 其他好文 > 详细

[LeetCode]The Skyline Problem

时间:2015-12-02 16:21:46      阅读:149      评论:0      收藏:0      [点我收藏+]

标签:

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;
    }
}

 

[LeetCode]The Skyline Problem

标签:

原文地址:http://www.cnblogs.com/vision-love-programming/p/5013203.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!