Problem
FAANG · LeetCode #218

The Skyline Problem — three-stage drill

1The ProblemHard

The Skyline Problem

Trace the silhouette of a city of overlapping buildings — return the key (x, height) points where the outline changes.

a city outline — trace the silhouette (skyline) of overlapping buildingsA h=10B h=15C h=1202468101214return[[2,10],[3,15],[7,12],[12,0]]
Examples
[[2,9,10],[3,7,15],[5,12,12]]→ [[2,10],[3,15],[7,12],[12,0]][[0,2,3],[2,5,3]]→ [[0,3],[5,0]]

constraintOutput is a list of [x, h] key points marking where the silhouette height changes. The last point has height 0.

2The PatternTIME O(n log n) · SPACE O(n)

Intervals

Given a pile of ranges, sort them by start — now any overlap is a neighbour, so a single left→right pass handles them.

sort the ranges by start, then sweep once01234567891011[7,9][2,5][1,3][8,10]given: a pile of ranges in any order
Problems using this pattern
3The SolutionRESULT → [[2,10],[3,15],[7,12],[12,0]]

Step through it

given — 3 overlapping rectangles. the outline is the union's upper boundary.A h=10B h=15C h=1202468101214active∅max = 0
given: 3 buildings · trace the silhouette (skyline)
Step 1 / 8
Arrow keys to navigate · escape to reset

All problems by pattern

Backtracking

Recursive Descent