3
def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int:4
# Set the initial height for the 1st building5
restrictions.append([1, 0])8
# Process restrictions to find the valid ones for reaching previous restrictions9
valid_restrictions_forward = []11
for id, ht in restrictions:12
current_diff = id - ht13
if current_diff >= temp_diff:14
temp_diff = current_diff15
valid_restrictions_forward.append([id, ht])17
# Process valid restrictions backward to find the ones for reaching next restrictions18
valid_restrictions_backward = []20
for id, ht in valid_restrictions_forward[::-1]:22
if current_sum <= temp_sum:23
temp_sum = current_sum24
valid_restrictions_backward.append([id, ht])26
# Reverse the backward valid restrictions to get the correct order27
valid_restrictions_backward.reverse()29
# Add maximum height for the last building due to the last restriction30
if valid_restrictions_backward[-1][0] != n:31
valid_restrictions_backward.append(34
valid_restrictions_backward[-1][1]36
- valid_restrictions_backward[-1][0],40
# Calculate the maximum height42
for i in range(len(valid_restrictions_backward) - 1):43
x1, y1 = valid_restrictions_backward[i]44
x2, y2 = valid_restrictions_backward[i + 1]45
available_height = (-x1 + y1 + x2 + y2) // 246
if available_height > max_height:47
max_height = available_height