Maximum Building Height Problem
Description
LeetCode Problem 1840.
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] = [id_i, maxHeight_i] indicates that building id_i must have a height less than or equal to maxHeight_i.
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.
Example 1:

Input: n = 5, restrictions = [[2,1],[4,1]]
Output: 2
Explanation: The green area in the image indicates the maximum allowed height for each building.
We can build the buildings with heights [0,1,2,1,2], and the tallest building has a height of 2.
Example 2:

Input: n = 6, restrictions = []
Output: 5
Explanation: The green area in the image indicates the maximum allowed height for each building.
We can build the buildings with heights [0,1,2,3,4,5], and the tallest building has a height of 5.
Example 3:

Input: n = 10, restrictions = [[5,3],[2,5],[7,4],[10,3]]
Output: 5
Explanation: The green area in the image indicates the maximum allowed height for each building.
We can build the buildings with heights [0,1,2,3,3,4,4,5,4,3], and the tallest building has a height of 5.
Constraints:
2 <= n <= 10^90 <= restrictions.length <= min(n - 1, 10^5)2 <= id_i <= nid_iis unique.0 <= maxHeight_i <= 10^9
Solution
Approach: Sorting and Two Passes
Add the constraint "building 1 has height 0", and add a final constraint for building n (at most n - 1) if there is none. Sort the constraints by building number.
A limit at one building also limits its neighbours, because heights may differ by at most 1 per step. So sweep left to right and cap each limit by the previous limit plus the distance between them, then sweep right to left and do the same. Now every limit is tight.
Between two adjacent constrained buildings at positions p1 < p2 with limits h1 and h2, the best skyline rises from h1 and falls to h2. If the peak is t, then (t - h1) + (t - h2) <= p2 - p1, so the peak is (h1 + h2 + p2 - p1) / 2, rounded down. The answer is the largest such peak over all adjacent pairs.
Complexity: O(m log m) time and O(m) space, where m is the number of restrictions.
class Solution:
def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int:
r = restrictions
r.append([1, 0])
r.sort()
if r[-1][0] != n:
r.append([n, n - 1])
m = len(r)
for i in range(1, m):
r[i][1] = min(r[i][1], r[i - 1][1] + r[i][0] - r[i - 1][0])
for i in range(m - 2, 0, -1):
r[i][1] = min(r[i][1], r[i + 1][1] + r[i + 1][0] - r[i][0])
ans = 0
for i in range(m - 1):
t = (r[i][1] + r[i + 1][1] + r[i + 1][0] - r[i][0]) // 2
ans = max(ans, t)
return ans