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
Maximum Building Height INPUT n = 4 buildings in a line B1 h=0 B2 max=1 B3 free B4 max=1 Restrictions Array: [2, 1] [4, 1] Rules: - Building 1 height = 0 - Adjacent diff <= 1 - Height >= 0 - Restrictions enforced ALGORITHM STEPS 1 Sort Restrictions Add [1,0], sort by id 2 Left-to-Right Pass h[i] = min(h[i], h[i-1]+dist) 0 --> 1 --> 1 3 Right-to-Left Pass h[i] = min(h[i], h[i+1]+dist) 0 <-- 1 <-- 1 4 Find Max Between Peak = (h1+h2+dist)/2 Between B2 and B4: dist = 4 - 2 = 2 peak = (1+1+2)/2 = 2 h=2 B2(h=1) B4(h=1) FINAL RESULT Optimal Building Heights B1 0 B2 1 B3 2 MAX B4 1 Output: 2 OK - All constraints met! Heights: [0, 1, 2, 1] Diff: 1, 1, 1 (all <= 1) B2 <= 1: OK, B4 <= 1: OK Key Insight: The greedy two-pass approach propagates height constraints in both directions. The maximum height between any two restricted buildings forms a triangle peak. Peak height = (h1 + h2 + distance) / 2, ensuring adjacent buildings differ by at most 1 while respecting all restrictions. TutorialsPoint - Maximum Building Height | Greedy Two-Pass Approach
Asked in
Google 15 Amazon 12 Microsoft 8 Meta 6
28.0K Views
Medium Frequency
~35 min Avg. Time
890 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