-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy path0120-triangle.py
More file actions
20 lines (16 loc) · 631 Bytes
/
0120-triangle.py
File metadata and controls
20 lines (16 loc) · 631 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# time complexity: O(n^2)
# space complexity: O(1)
from typing import List
class Solution:
def minimumTotal(self, triangle: List[List[int]]) -> int:
for r in range(1, len(triangle)):
for c in range(r + 1):
smallestAbove = float('inf')
if c > 0:
smallestAbove = triangle[r-1][c-1]
if c < r:
smallestAbove = min(smallestAbove, triangle[r-1][c])
triangle[r][c] += smallestAbove
return min(triangle[-1])
triangle = [[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]
print(Solution().minimumTotal(triangle))