The Skyline Problem

https://leetcode.com/problems/the-skyline-problem/description/

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).

Buildings Skyline Contour

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], ...]

Thoughts

这题思路是很直观的,遍历并检查每个点最高的高度是否有改变。每个点有什么高度由是由一进一出的范围决定的。扫描线专门用来处理“一进一出”。遍历并track最大自然和heap有关。 难点是多个楼start或end重合时, 先处理谁。当两个点都是start, 我们要只保留高的楼的高度, 因此应先把高楼放进去, 降序排列; 当两个点都是end, 我们要先remove矮楼高度, 因为先删掉高楼高度时当前最高会是矮楼, 而矮楼高度与之前存的最高高度不一样, 因此会把矮楼高度误存进结果。 当两边一进一出时, 应先处理进, 否则先把当前高楼删了后会出现不必要的高度为0的点。

Code

class Solution {
    class Height {
        int height, pos;
        boolean arrive;
        Height(int h, int pos, boolean arrive) {
            this.height = h;
            this.pos = pos;
            this.arrive = arrive;
        }
    }
    public List<List<Integer>> getSkyline(int[][] buildings) {
        List<Height> heights = new ArrayList<>();
        for (int[] b : buildings) {
            heights.add(new Height(b[2], b[0], true));
            heights.add(new Height(b[2], b[1], false));
        }
        Collections.sort(heights, (a, b) -> {
            if (a.pos != b.pos) return a.pos - b.pos;
            if (a.arrive && b.arrive) return b.height - a.height;
            else if (!a.arrive && !b.arrive) return a.height - b.height;
            else return a.arrive ? -1 : 1;
        });
        PriorityQueue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));
        List<List<Integer>> res = new ArrayList<>();
        pq.offer(0);
        int premax = 0;
        for (Height h : heights) {
            if (h.arrive) pq.offer(h.height);
            else pq.remove(h.height);
            if (pq.peek() != premax) {
                List<Integer> r = new ArrayList<>();
                r.add(h.pos);
                r.add(pq.peek());
                res.add(r);
                premax = pq.peek();
            }
        }
        return res;
    }
}

Analysis

排序和建堆时间复杂度O(NlgN).

Last updated