Leetcode 0218: The Skyline Problem

Title 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. Given the locations and heights of all the buildings, return the skyline formed by these buildings collectively.

The geometric information of each building is given in the array buildings where buildings[i] = [lefti, righti, heighti]:
1 left_i is the x coordinate of the left edge of the ith building.
2 right_i is the x coordinate of the right edge of the ith building.
3 height_i is the height of the ith building.

You may assume all buildings are perfect rectangles grounded on an absolutely flat surface at height 0.
The skyline should be represented as a list of “key points” sorted by their x-coordinate in the form [[x1,y1],[x2,y2],…]. Each key point is the left endpoint of some horizontal segment in the skyline except the last point in the list, which always has a y-coordinate 0 and is used to mark the skyline’s termination where the rightmost building ends. Any ground between the leftmost and rightmost buildings should be part of the skyline’s contour.

Note: 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],…]

Chinese description:

The skyline of a city is the outer contour of the outline formed by all the buildings in the city viewed from a distance. Give you the location and height of all the buildings, please return to the skyline formed by these buildings.

The geometric information of each building is represented by the array buildings, where the triplet buildings[i] = [lefti, righti, heighti] means:
1 left_i is the x coordinate of the left edge of the i-th building.
2 right_i is the x coordinate of the right edge of the i-th building.
3 height_i is the height of the i-th building.
The skyline should be expressed as a list of "key points" in the format [[x1,y1],[x2,y2],...], and sorted by the x coordinate. The key point is the left end of the horizontal line segment. The last point in the list is the end point of the rightmost building. The y coordinate is always 0 and is only used to mark the end point of the skyline. In addition, the ground between any two adjacent buildings should be considered part of the skyline outline.

Note: There must be no continuous horizontal lines of the same height in the output skyline. For example, […[2 3], [4 5], [7 5], [11 5], [12 7]…] are incorrect answers; three lines with a height of 5 should be merged into one in the final output: […[2 3], [4 5], [12 7], …]

Example 1:
Insert picture description here

Input: buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]
Output: [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]
Explanation:
Figure A shows the buildings of the input.
Figure B shows the skyline formed by those buildings. The red points in figure B represent the key points in the output list.

Example 2:

Input: buildings = [[0,2,3],[2,5,3]]
Output: [[0,3],[5,0]]

Constraints:

1 <= buildings.length <= 104
0 <= left_i < right_i <= 231 - 1
1 <= height_i <= 231 - 1
buildings is sorted by lefti in non-decreasing order.

Time complexity: O(n 2 )
scan line method:
treat each building as two vertical lines on the x-axis, the left end means the beginning and the height is counted as a negative number, and the right end means the end height is a positive number. Use the scan line to initialize a maximum heap (the top of the heap is the maximum height), sweep from left to right, if you encounter the left end (the end with a negative height), then enter the heap, if you encounter the right end (the height is positive) Number of endpoints), delete it from the heap. Use the prev variable to record the last turning point. Every time when the value of the top of the pile is not equal to the value of the previous turning point, the current endpoint and height are recorded and stored in the result.
Time complexity analysis: traverse each endpoint O(n), and each time in the traversal process, it must be added to or deleted from the heap (PriorityQueue). For Java PriorityQueue, find the element O(n), and heapify O(logn). So it costs O(n) to delete the specified element from the heap. Total O(n 2 ).

class Solution {
    
    
    public List<List<Integer>> getSkyline(int[][] buildings) {
    
    
        List<List<Integer>> res = new ArrayList<>();
        List<int[]> height = new ArrayList<>();
        for(int[] b:buildings) {
    
    
            height.add(new int[]{
    
    b[0], -b[2]}); // left edge
            height.add(new int[]{
    
    b[1], b[2]}); // right edgs
        }
        Collections.sort(height, (a, b) -> {
    
    
            if(a[0] != b[0]) 
                return a[0] - b[0];
            return a[1] - b[1];
        });
        Queue<Integer> pq = new PriorityQueue<>((a, b) -> (b - a));
        pq.offer(0);
        int prev = 0;
        for(int[] h:height){
    
    
            if(h[1] < 0){
    
    
                // left edge
                pq.offer(-h[1]);
            }else{
    
    
                pq.remove(h[1]);
            }
            int cur = pq.peek();
            if(prev != cur) {
    
    
                res.add(Arrays.asList(h[0], cur));
                prev = cur;
            }
        }
        return res;
    }
}

Guess you like

Origin blog.csdn.net/weixin_43946031/article/details/113831508