Maximum Building Height - Problem
City Skyline Construction

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
City Skyline Construction123MAX45PEAK๐Ÿ—๏ธ Adjacent buildings can only differ by 1 in height๐Ÿšซ Red buildings have height restrictions๐ŸŽฏ Goal: Find the maximum possible building height
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.
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
34.3K Views
Medium Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen