-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path2865-beautiful-towers-i.py
More file actions
31 lines (27 loc) · 1.04 KB
/
2865-beautiful-towers-i.py
File metadata and controls
31 lines (27 loc) · 1.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# time complexity: O(n^2)
# space complexity: O(1)
from typing import List
class Solution:
def maximumSumOfHeights(self, heights: List[int]) -> int:
result = float('-inf')
for i in range(len(heights)):
tempHeights = list(heights)
total = heights[i]
for j in range(i - 1, -1, -1):
tempHeights[j] = min(tempHeights[j + 1], tempHeights[j])
total += tempHeights[j]
for j in range(i + 1, len(heights)):
tempHeights[j] = min(tempHeights[j - 1], tempHeights[j])
total += tempHeights[j]
result = max(result, total)
return result
heights = [5, 3, 4, 1, 1]
print(Solution().maximumSumOfHeights(heights))
heights = [6, 5, 3, 9, 2, 7]
print(Solution().maximumSumOfHeights(heights))
heights = [3, 2, 5, 5, 2, 3]
print(Solution().maximumSumOfHeights(heights))
heights = [2, 4, 5, 2, 5, 5, 2, 1, 1, 3]
print(Solution().maximumSumOfHeights(heights))
heights = [5, 5, 3, 2, 6]
print(Solution().maximumSumOfHeights(heights))