-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path120. Triangle.py
More file actions
28 lines (19 loc) · 1000 Bytes
/
120. Triangle.py
File metadata and controls
28 lines (19 loc) · 1000 Bytes
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
def minimumTotal(triangle) -> int: # solution using tabulation (bottom up)
for i in range(1, len(triangle)):
for j in range(len(triangle[i])):
if j == 0:
triangle[i][j] += triangle[i-1][j] # left column
elif j == i: # right column
triangle[i][j] += triangle[i-1][j-1]
else:
triangle[i][j] += min(triangle[i - 1][j - 1], triangle[i-1][j])
return min(triangle[-1])
def flip_triangle_solution(triangle) -> int:
# smarter solution
# flipping the triangle upside-down and applying the same logic will make the code much easier
# you don't have to worry about the cases where it goes out of bounds
for i in reversed(range(len(triangle)-1)): # start from bottom
for j in range(len(triangle[i])):
triangle[i][j] += min(triangle[i+1][j], triangle[i+1][j+1])
return triangle[0]
print(flip_triangle_solution(triangle=[[2], [3, 4], [6, 5, 7], [4, 1, 8, 3]]))