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]:
leftiis the x coordinate of the left edge of theithbuilding.rightiis the x coordinate of the right edge of theithbuilding.heightiis the height of theithbuilding.
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],...]
Example 1:
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 <= 1040 <= lefti < righti <= 231 - 11 <= heighti <= 231 - 1buildingsis sorted byleftiin non-decreasing order.
Approach: Sweep Line with a Max-Heap
Algorithm
- Convert every building into two events:
(left, -height)for the start and(right, height)for the end, so starts and ends are distinguishable - Sort all events by x-coordinate; when two events share an x, process the higher start first and the end of a taller building before a shorter one
- Sweep the events left to right, using a max-heap to track the heights of all currently active buildings
- For a start event, push the height; for an end event, remove the height from the heap
- After processing each x, compare the current maximum height with the previous one; if it changed, a new key point
(x, maxHeight)is produced
Time & Space Complexity
- Time Complexity: O(n log n) - sorting the 2n events plus heap operations
- Space Complexity: O(n) - the event list and the active-height heap
Java Implementation
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.PriorityQueue;
public class TheSkylineProblem {
public List<List<Integer>> getSkyline(int[][] buildings) {
List<List<Integer>> result = new ArrayList<>();
List<int[]> height = new ArrayList<>();
for (int[] b : buildings) {
height.add(new int[]{b[0], -b[2]}); // start event
height.add(new int[]{b[1], b[2]}); // end event
}
Collections.sort(height, (a, b) ->
a[0] != b[0] ? a[0] - b[0] : a[1] - b[1]);
PriorityQueue<Integer> pq = new PriorityQueue<>(Collections.reverseOrder());
pq.offer(0);
int prev = 0;
for (int[] h : height) {
if (h[1] < 0) {
pq.offer(-h[1]); // building starts
} else {
pq.remove(h[1]); // building ends
}
int cur = pq.peek();
if (cur != prev) {
result.add(Arrays.asList(h[0], cur));
prev = cur;
}
}
return result;
}
// Test method
public static void main(String[] args) {
TheSkylineProblem solver = new TheSkylineProblem();
int[][] buildings = {{2, 9, 10}, {3, 7, 15}, {5, 12, 12}, {15, 20, 10}, {19, 24, 8}};
System.out.println(solver.getSkyline(buildings));
// Expected: [[2, 10], [3, 15], [7, 12], [12, 0], [15, 10], [20, 8], [24, 0]]
}
}
Example Walkthrough
For buildings = [[2,9,10],[3,7,15],[5,12,12]]:
- Events:
(2,-10),(3,-15),(5,-12),(7,15),(9,10),(12,12) - At x=2, heap becomes
{10}; max changes 0 -> 10, key point[2,10] - At x=3, heap becomes
{15,10}; max changes 10 -> 15, key point[3,15] - At x=5, heap becomes
{15,12,10}; max stays 15, no key point - At x=7, height 15 ends; heap
{12,10}; max changes 15 -> 12, key point[7,12] - At x=9, height 10 ends; heap
{12}; max stays 12, no key point - At x=12, height 12 ends; heap
{0}; max changes 12 -> 0, key point[12,0]
Key Points
- Event Encoding: A negative height marks the left edge, a positive one the right edge
- Sort Order: At the same x, taller buildings start first and taller buildings end last, keeping the active set consistent
- Max-Heap: The peek always reflects the current skyline height
- Only Emit on Change: A key point is produced exactly when the current max height differs from the previous one, which also merges consecutive equal-height lines
- O(n log n): The sweep line avoids the O(n²) of checking every building against every point