Maximum Building Height - Problem
You want to build n new buildings in a city. The new buildings will be built in a line and are labeled from 1 to n.
However, there are city restrictions on the heights of the new buildings:
- The height of each building must be a non-negative integer.
- The height of the first building must be 0.
- The height difference between any two adjacent buildings cannot exceed 1.
Additionally, there are city restrictions on the maximum height of specific buildings. These restrictions are given as a 2D integer array restrictions where restrictions[i] = [idi, maxHeighti] indicates that building idi must have a height less than or equal to maxHeighti.
It is guaranteed that each building will appear at most once in restrictions, and building 1 will not be in restrictions.
Return the maximum possible height of the tallest building.
Input & Output
Example 1 — Basic Case with Restriction
$
Input:
n = 4, restrictions = [[2,1],[4,1]]
›
Output:
2
💡 Note:
Building heights: [0, 1, 0, 1]. Building 1 starts at 0, building 2 restricted to max 1, building 3 can be at most height 1+1=2 but must support building 4's max height of 1, so building 3 gets height 1+1-1=1, but actually building 3 can be height 2. Final optimal: [0, 1, 2, 1] with max height 2.
Example 2 — No Restrictions
$
Input:
n = 5, restrictions = []
›
Output:
2
💡 Note:
Without restrictions, optimal heights are [0, 1, 2, 1, 0] or [0, 1, 2, 3, 2]. The maximum reachable height is limited by the need to return back considering adjacent differences, giving maximum height of 2 at position 3.
Example 3 — Multiple Restrictions
$
Input:
n = 6, restrictions = [[3,0],[4,2]]
›
Output:
1
💡 Note:
Building 3 must be height 0, building 4 max height 2. Starting from building 1 at height 0, we can reach building 3 with height 0, then building 4 can be at most min(0+1, 2) = 1. Maximum achievable height is 1.
Constraints
- 1 ≤ n ≤ 109
- 0 ≤ restrictions.length ≤ 1000
- 1 ≤ idi ≤ n
- 0 ≤ maxHeighti ≤ 109
Visualization
Tap to expand
Understanding the Visualization
1
Input
n buildings with height restrictions and adjacency rules
2
Process
Two-pass optimization respecting all constraints
3
Output
Maximum height of tallest building
Key Takeaway
🎯 Key Insight: Two-pass optimization handles bidirectional adjacency constraints optimally
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code