LeetCode Top Interview Questions 218. The Skyline Problem (Java版; Hard)

welcome to my blog

LeetCode Top Interview Questions 218. The Skyline Problem (Java版; 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], ...]
第一次做;将输入的数据进行排序;核心:遇到start进入堆, 遇到end离开堆; 进入堆或者离开堆后, 如果堆的最大值发生改变, 则更新res; 看下面的3个edge situations; 总结成一句话: 左边界进, 右边界处, 最大值发生变化则更新; 3个edge situations的第三个最复杂
class Solution {
    public List<List<Integer>> getSkyline(int[][] buildings) {
        List<List<Integer>> res = new ArrayList<>();
        List<int[]> coor_height = new ArrayList<>();
        for(int[] b : buildings){
            //核心:通过正负值区分start和end, 同时也满足3个edge situations的比较要求
            //b[0],b[1]是建筑的坐标; b[2]是建筑的高度; 正高度表示start, 也就是建筑的左边界; 负高度表示end, 也就是建筑的右边界
            coor_height.add(new int[]{b[0], b[2]});
            coor_height.add(new int[]{b[1], -b[2]});
        }
        //两个建筑坐标相等的话, 高度更高的建筑排在前面
        //两个建筑坐标不相等的话, 坐标更靠前的建筑排在前面
        Collections.sort(coor_height, (a, b)->(a[0]==b[0])?(b[1]-a[1]) : (a[0]-b[0]));
        //
        //key是高度, value是该高度出现的次数; 
        //Collections.reverseOrder()是最大的key排在最前面
        TreeMap<Integer, Integer> heightMap = new TreeMap<>(Collections.reverseOrder());
        heightMap.put(0, 1);
        //pre表示上一次的最大高度
        int pre = 0;
        for(int[] h:coor_height){
            int cur = h[1];
            //如果当前位置是建筑的左边界
            if(cur>0){
                Integer cnt = heightMap.get(cur);
                cnt = cnt==null? 1 : cnt+1;
                heightMap.put(cur,cnt);
            }
            //如果当前位置是建筑的右边界, 从堆中删除相应的该建筑的记录, 注意用-cur, 因为heightMap中是建筑高度的正值
            else{
                Integer cnt = heightMap.get(-cur);
                //细节:cnt==1时, 删除该高度在TreeMap中的记录
                if(cnt==1)
                    heightMap.remove(-cur);
                else
                    heightMap.put(-cur, cnt-1);
            }
            //如果当前最大值和pre不同, 则更新res
            int max = heightMap.firstKey();
            if(max!=pre){
                res.add(new ArrayList<Integer>());
                res.get(res.size()-1).add(h[0]);
                //细节:这里需要用max
                res.get(res.size()-1).add(max);
                pre = max;
            }
        }
        return res;
    }
}
核心: 比较器比较规则来源: 3个edge situations, 这三个特殊情况决定了建筑之间的排序方式; 第三个特例最复杂, 图中画的是左高右低, 需要先让右建筑的start排在前面(也就是先让右建筑的start先进入TreeMap), 如果是左高右低,同样需要先让右建筑的start排在前面, 所以可以让建筑的end对应的高度为负数,然后从小到大排, 这样当两个建筑的坐标一样时, 值更大的排在前面; 这样的比较规则也满足前两种特殊情况 这个人讲的非常清楚!
  1. 两个建筑的start一样, 此时需要让height更高的建筑排在前面
  2. 两个建筑的end一样, 此时需要让height更低的建筑排在前面
  3. 一个建筑的start和另一个建筑的end一样, 此时需要让height更高的建筑排在前面
    将建筑的start变成负数
LeetCode最优解; 不使用PriorityQueue, 因为PriorityQueue的remove操作是O(N)的, 比较慢, 所以使用TreeMap, TreeMap的remove操作是O(NlogN)的, 更快! 作者做start变成负数, 所以比较器中写的是a[1]-b[1]
public class Solution {
    public List<int[]> getSkyline(int[][] buildings) {
        List<int[]> heights = new ArrayList<>();
        for (int[] b: buildings) {
            heights.add(new int[]{b[0], - b[2]});
            heights.add(new int[]{b[1], b[2]});
        }
        Collections.sort(heights, (a, b) -> (a[0] == b[0]) ? a[1] - b[1] : a[0] - b[0]);
        TreeMap<Integer, Integer> heightMap = new TreeMap<>(Collections.reverseOrder());
        heightMap.put(0,1);
        int prevHeight = 0;
        List<int[]> skyLine = new LinkedList<>();
        for (int[] h: heights) {
            if (h[1] < 0) {
                Integer cnt = heightMap.get(-h[1]);
                cnt = ( cnt == null ) ? 1 : cnt + 1;
                heightMap.put(-h[1], cnt);
            } else {
                Integer cnt = heightMap.get(h[1]);
                if (cnt == 1) {
                    heightMap.remove(h[1]);
                } else {
                    heightMap.put(h[1], cnt - 1);
                }
            }
            int currHeight = heightMap.firstKey();
            if (prevHeight != currHeight) {
                skyLine.add(new int[]{h[0], currHeight});
                prevHeight = currHeight;
            }
        }
        return skyLine;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值