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:
0.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.
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 <= 1090 <= restrictions.length <= min(n - 1, 105)2 <= idi <= nidi is unique.0 <= maxHeighti <= 109When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you need to build the tallest possible structures at specific locations while making sure no building is too much taller than its neighbor. The brute force approach involves trying every conceivable height for every single building and checking which combination works best.
Here's how the algorithm would work step-by-step:
def max_building_height(num_buildings, restrictions):
# Sort restrictions by building index to properly evaluate neighbor differences
sorted_restrictions = sorted(restrictions, key=lambda item: item[0])
full_restrictions = [[1, 0]] + sorted_restrictions
if full_restrictions[-1][0] < num_buildings:
full_restrictions.append([num_buildings, num_buildings - 1])
max_possible_height = 0
# Recursively or iteratively generate all combinations to find the maximum possible valid height
def evaluate_combinations(current_index, current_heights):
nonlocal max_possible_height
if current_index == len(full_restrictions):
current_max = max(current_heights)
if current_max > max_possible_height:
max_possible_height = current_max
return
target_index, height_limit = full_restrictions[current_index]
previous_index, previous_height = full_restrictions[current_index - 1] if current_index > 0 else [1, 0]
max_jump = target_index - previous_index
for assigned_height in range(height_limit + 1):
if current_index == 0 or abs(assigned_height - previous_height) <= max_jump:
evaluate_combinations(current_index + 1, current_heights + [assigned_height])
# Kick off the exhaustive search starting from the first building restriction
evaluate_combinations(0, [])
return max_possible_height
Instead of checking every single building one by one, the optimal approach first sorts the known height restrictions and adds the ground at the very beginning and the final building at the very end. Then, it makes two passes through the restrictions to figure out how much buildings are allowed to grow based on their neighbors.
Here's how the algorithm would work step-by-step:
class Solution:
def max_building(self, total_buildings: int, restrictions: list[list[int]]) -> int:
restrictions.append([1, 0])
restrictions.append([total_buildings, total_buildings - 1])
restrictions.sort(key=lambda item: item[0])
# Ensure left-to-right neighbor heights do not exceed slope limits.
for index in range(1, len(restrictions)):
previous_index = index - 1
max_allowed_height = restrictions[previous_index][1] + (restrictions[index][0] - restrictions[previous_index][0])
if restrictions[index][1] > max_allowed_height:
restrictions[index][1] = max_allowed_height
# Ensure right-to-left neighbor heights do not exceed slope limits.
for index in range(len(restrictions) - 2, -1, -1):
next_index = index + 1
max_allowed_height = restrictions[next_index][1] + (restrictions[next_index][0] - restrictions[index][0])
if restrictions[index][1] > max_allowed_height:
restrictions[index][1] = max_allowed_height
ultimate_maximum_height = 0
# Evaluate peak heights achievable in open gaps between restrictions.
for index in range(1, len(restrictions)):
left_building = restrictions[index - 1]
right_building = restrictions[index]
horizontal_distance = right_building[0] - left_building[0]
height_difference = abs(right_building[1] - left_building[1])
peak_height = (horizontal_distance + left_building[1] + right_building[1]) // 2
if peak_height > ultimate_maximum_height:
ultimate_maximum_height = peak_height
return ultimate_maximum_height| Case | How to Handle |
|---|---|
| Restrictions list is empty | Assume only the first building has a height of 0 and the last building n can reach n minus 1 by incrementing by 1 each step. |
| Restriction provided for building 1 with height greater than 0 | Clamp the height of building 1 to 0 since the problem explicitly states the heights must start at 0 for the first building. |
| Duplicate restrictions for the same building index | Preprocess the restrictions by keeping only the minimum height restriction for any duplicate building index. |
| Restrictions do not cover the last building n | Append a virtual restriction at building n with an effectively infinite height to bound the final slope. |
| Very large values of n up to 10 to the 9th power | Sort only the given restrictions and iterate through them instead of allocating an array of size n to ensure optimal space and time efficiency. |
| Steep height restrictions that force adjacent buildings to drop faster than the slope of 1 allows | Perform a two-pass left-to-right and right-to-left propagation to strictly enforce the maximum allowable height difference of 1 between adjacent buildings. |
| Extremely tall restrictions that exceed the distance between buildings | Mathematical propagation naturally caps the peak height by the midpoint formula between adjacent restricted buildings. |