Skip to content

Commit 72eae2a

Browse files
authored
Create maximum-building-height.py
1 parent ee96ec4 commit 72eae2a

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

Python/maximum-building-height.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Time: O(nlogn)
2+
# Space: O(1)
3+
4+
class Solution(object):
5+
def maxBuilding(self, n, restrictions):
6+
"""
7+
:type n: int
8+
:type restrictions: List[List[int]]
9+
:rtype: int
10+
"""
11+
restrictions.extend([[1, 0], [n, n-1]])
12+
restrictions.sort()
13+
for i in reversed(xrange(len(restrictions)-1)):
14+
restrictions[i][1] = min(restrictions[i][1], restrictions[i+1][1]+(restrictions[i+1][0]-restrictions[i][0]))
15+
result = 0
16+
for i in xrange(1, len(restrictions)):
17+
restrictions[i][1] = min(restrictions[i][1], restrictions[i-1][1]+(restrictions[i][0]-restrictions[i-1][0]))
18+
left, h1 = restrictions[i-1]
19+
right, h2 = restrictions[i]
20+
result = max(result, max(h1, h2)+((right-left)-abs(h1-h2))//2)
21+
return result

0 commit comments

Comments
 (0)