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]

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


Test Cases

Input:

(int[][]) buildings = [[2,9,10],[3,7,15],[5,12,12],[15,20,10],[19,24,8]]

Output:

(int[][]) [[2,10],[3,15],[7,12],[12,0],[15,10],[20,8],[24,0]]

Solution

class Solution {
    public List<List<Integer>> getSkyline(int[][] buildings) {
        int totalBuildings = buildings.length;
        BuildingPoint[] buildingPoints = new BuildingPoint[totalBuildings*2];
        int index = 0;
        for(int[] building: buildings) {
            buildingPoints[index++] = new BuildingPoint(building[0], building[2], true);
            buildingPoints[index++] = new BuildingPoint(building[1], building[2], false);
        }
        Arrays.sort(buildingPoints);

        TreeMap<Integer, Integer> queue = new TreeMap<>();
        queue.put(0, 1);
        int prevMaxHeight = 0;
        List<List<Integer>> result = new ArrayList<>();

        for(BuildingPoint buildingPoint: buildingPoints) {
            if (buildingPoint.isStart) {
                queue.compute(buildingPoint.y, (key, value) -> value != null ? value + 1: 1);
            } else {
                queue.compute(buildingPoint.y, (key, value) -> value == 1 ? null: value - 1);
            }
            int currentMaxHeight = queue.lastKey();
            if (prevMaxHeight != currentMaxHeight) {
                result.add(Arrays.asList(buildingPoint.x, currentMaxHeight));
                prevMaxHeight = currentMaxHeight;
            }
        }
        return result;
    }
}

class BuildingPoint implements Comparable<BuildingPoint>{
    int x;
    int y;
    boolean isStart;
    BuildingPoint(int x, int y, boolean isStart) {
        this.x = x;
        this.y = y;
        this.isStart = isStart;
    }

    public  int compareTo(BuildingPoint o) {
        if (this.x != o.x) {
            return this.x - o.x;
        }
        // if two starts are compared, then higher y should be picked first
        // if two ends are compared, then lower y should be picked first
        // if one start and one end is compared, then start should appear before end
        int h1 = isStart ? -y : y;
        int h2 = o.isStart ? -o.y : o.y;
        return h1 - h2;
    }

    public String toString() {
        return "{"+x+", "+y+", "+isStart+"}";
    }
}
Time Complexity: O(nlogn)
Space Complexity: O(n)