1840. Maximum Building Height - cocoder39/coco39_LC GitHub Wiki
1840. Maximum Building Height
- 2 scans to propagate restriction at index i to index i+1 and vice verse. This way we can determine the upper bound of each restricted index.
- determine the max hight between 2 restricted indexes
- h1 + x == h2 + y = max_height => x - y = h2 - h1
- x + y = right_index - left_index
- x = (h2 - h1 + right_index - left_index) / 2
- h = h1 + x = (h2 + h1 + right_index - left_index) / 2
class Solution:
def maxBuilding(self, n: int, restrictions: List[List[int]]) -> int:
restrictions.extend([1, 0], [n, n-1](/cocoder39/coco39_LC/wiki/1,-0],-[n,-n-1))
restrictions.sort()
m = len(restrictions)
for i in range(1, m):
restrictions[i][1] = min(restrictions[i][1], restrictions[i-1][1] + restrictions[i][0] - restrictions[i-1][0])
for i in range(m-2, -1, -1):
restrictions[i][1] = min(restrictions[i][1], restrictions[i+1][1] + restrictions[i+1][0] - restrictions[i][0])
res = 0
for i in range(1, m):
left, h1 = restrictions[i-1]
right, h2 = restrictions[i]
res = max(res, (h1+h2+right-left) // 2)
return res