leetcode218 - The Skyline Problem - hard

A city's skyline is the outer contour of the silhouette formed by all the buildings in that city when viewed from a distance. Now suppose you are given the locations and height of all the buildings as shown on a cityscape photo (Figure A), write a program to output the skyline formed by these buildings collectively (Figure B).

The geometric information of each building is represented by a triplet of integers [Li, Ri, Hi], where Li and Ri are the x coordinates of the left and right edge of the ith building, respectively, and Hi is its height. It is guaranteed that 0 ≤ Li, Ri ≤ INT_MAX, 0 < Hi ≤ INT_MAX, and Ri - Li > 0. You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
For instance, the dimensions of all buildings in Figure A are recorded as: [ [2 9 10], [3 7 15], [5 12 12], [15 20 10], [19 24 8] ] .
The output is a list of "key points" (red dots in Figure B) in the format of [ [x1,y1], [x2, y2], [x3, y3], ... ] that uniquely defines a skyline. A key point is the left endpoint of a horizontal line segment. Note that the last key point, where the rightmost building ends, is merely used to mark the termination of the skyline, and always has zero height. Also, the ground in between any two adjacent buildings should be considered part of the skyline contour.
For instance, the skyline in Figure B should be represented as:[ [2 10], [3 15], [7 12], [12 0], [15 10], [20 8], [24, 0] ].
Notes:
* The number of buildings in any input list is guaranteed to be in the range [0, 10000].
* The input list is already sorted in ascending order by the left x position Li.
* The output list must be sorted by the x position.
* There must be no consecutive horizontal lines of equal height in the output skyline. For instance, [...[2 3], [4 5], [7 5], [11 5], [12 7]...] is not acceptable; the three lines of height 5 should be merged into one in the final output as such: [...[2 3], [4 5], [12 7], …]

 

max Heap。(PriorityQueue Or TreeSet under one constraint)
1.把每行输入转化为头尾两个点。Point(x, y, isStart)。
2.把所有点排序。(主要x轴排序,若x重合,是一头一尾则先头,都是头则先高,都是尾则先矮。(通过画图看正误))
3.生成一个对Integer对象的maxHeap。用于放高度值的。
4.对所有点遍历:如果是头:将高度推入heap,若peek变了则给结果增加[x, peek()]。如果是尾:将该高度从heap移除(注意是remove不是poll()!),若peek变了则给结果增加[x, peek()]。

原理:
加入头的时候,如果忽然把水平线拔高了,说明现在没有人能shadow我这个点,那我鹤立鸡群是一个标记点了。
推出尾的时候,如果忽然把水平线拉低了,说明我刚才是最高的,现在跑到曾经的第二高的地方了,也是一个标记点(反而推出尾的时候如果水平线毫无变化,说明上面有一个更高的一直笼罩着我,我被shadow着,走不走别人看不到,不是特征点)。

细节:
1.本题遇到尾是remove不是poll!另外注意PQ的remove时间复杂度是O(n),所以用TreeSet会快一点。然而TreeSet有一个致命伤是它不能有重复元素,那你遇到两个紧紧贴着的出入点比如[0,2,3][2,5,3]的话,就会出错。(先处理入点,这时想加入一个同样的高度却并没有加进去,到时候remove的时候倒是都想remove。)
2.函数外写比较器,头是private class PointComparator implements Comparator<Point>。第二个是class不是类型名Comparator<Point>。然后对list用这个比较器排序的话是Collections.sort(list, new PointComparator());先写list对象再写比较器。
3.快速产生数组的写法:list.add(new int[] {p.x, maxHeap.peek()} );

 

实现:

class Solution {
    private class Point {
        public int x;
        public int y;
        public boolean isStart;
        public Point(int x, int y, boolean isStart) {
            this.x = x;
            this.y = y;
            this.isStart = isStart;
        }
    }
    
    private class PointComparator implements Comparator<Point> {
        @Override
        public int compare(Point a, Point b) {
            if (a.x != b.x) {
                return a.x - b.x;
            } else if (a.isStart != b.isStart) {
                return a.isStart ? -1 : 1;
            } else if (a.isStart) {
                return b.y - a.y;
            } else {
                return a.y - b.y;
            }
        }
    }
    
    public List<int[]> getSkyline(int[][] buildings) {
        if (buildings == null || buildings.length == 0 || buildings[0].length != 3) {
            return new ArrayList<int[]>();
        }
        
        PriorityQueue<Integer> maxHeap = new PriorityQueue<>(Collections.reverseOrder());
        List<Point> list = new ArrayList<>();
        List<int[]> ans = new ArrayList<>();
        for (int i = 0; i < buildings.length; i++) {
            list.add(new Point(buildings[i][0], buildings[i][2], true));
            list.add(new Point(buildings[i][1], buildings[i][2], false));
        }
        // P1: check API.
        Collections.sort(list, new PointComparator());
        
        maxHeap.offer(0);
        for (int i = 0; i < list.size(); i++) {
            Point p = list.get(i);
            int temp = maxHeap.peek();
            if (p.isStart) {
                maxHeap.offer(p.y);
            } else {
                maxHeap.remove(p.y);
            }
            if (maxHeap.peek() != temp) {
                // P2: check API.
                ans.add(new int[] {p.x, maxHeap.peek()});
            }
        }
        return ans;
    }
}

 

转载于:https://www.cnblogs.com/jasminemzy/p/9661775.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值