Maximum Building Height - Problem
City Skyline Construction
You're tasked with designing the skyline for a new city district! You need to build
However, the city has strict zoning laws:
๐๏ธ Basic Rules:
โข All building heights must be non-negative integers
โข Building 1 (the first building) must have height 0 (ground level)
โข Adjacent buildings can't differ in height by more than 1 (for structural stability)
๐ซ Height Restrictions:
Some buildings have maximum height limits due to flight paths, view corridors, etc. These are given as
Goal: Return the maximum possible height of the tallest building in your skyline design.
Example: With 5 buildings and restriction
You're tasked with designing the skyline for a new city district! You need to build
n new buildings in a straight line, labeled from 1 to n.However, the city has strict zoning laws:
๐๏ธ Basic Rules:
โข All building heights must be non-negative integers
โข Building 1 (the first building) must have height 0 (ground level)
โข Adjacent buildings can't differ in height by more than 1 (for structural stability)
๐ซ Height Restrictions:
Some buildings have maximum height limits due to flight paths, view corridors, etc. These are given as
restrictions[i] = [buildingId, maxHeight]Goal: Return the maximum possible height of the tallest building in your skyline design.
Example: With 5 buildings and restriction
[3, 2] (building 3 โค 2), you could build: [0, 1, 2, 1, 2] with max height 2. Input & Output
example_1.py โ Basic Case
$
Input:
n = 5, restrictions = [[2, 1], [4, 1]]
โบ
Output:
2
๐ก Note:
Buildings can be [0, 1, 0, 1, 2]. Building 2 is limited to height 1, building 4 is limited to height 1. The optimal configuration gives max height 2 at building 5.
example_2.py โ Single Restriction
$
Input:
n = 6, restrictions = [[3, 2]]
โบ
Output:
3
๐ก Note:
Buildings can be [0, 1, 2, 3, 2, 1]. Building 3 is restricted to height โค 2, so we build up to it and continue from there. Max height is 3 at building 4.
example_3.py โ No Restrictions
$
Input:
n = 4, restrictions = []
โบ
Output:
3
๐ก Note:
Without restrictions, we can build [0, 1, 2, 3], achieving maximum height 3 at the last building.
Constraints
- 1 โค n โค 109
- 0 โค restrictions.length โค 105
- 2 โค idi โค n
- 0 โค maxHeighti โค 109
- Each building appears at most once in restrictions
- Building 1 will not be in restrictions (always height 0)
Visualization
Tap to expand
Understanding the Visualization
1
Start at ground level
Building 1 must be at height 0 (sea level)
2
Add restrictions
Some buildings have maximum height limits (flight paths)
3
Forward propagation
Calculate how high we can reach from the left
4
Backward propagation
Apply constraints flowing from the right
5
Find optimal peak
The highest point achievable under all constraints
Key Takeaway
๐ฏ Key Insight: Use constraint propagation in both directions to find the optimal height that satisfies all restrictions while maximizing the skyline peak.
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code