-
Notifications
You must be signed in to change notification settings - Fork 613
/
120.py
39 lines (32 loc) · 1.1 KB
/
120.py
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
32
33
34
35
36
37
38
39
'''
Given a triangle, find the minimum path sum from top to bottom. Each step you may move to adjacent numbers on the row below.
For example, given the following triangle
[
[2],
[3,4],
[6,5,7],
[4,1,8,3]
]
The minimum path sum from top to bottom is 11 (i.e., 2 + 3 + 5 + 1 = 11).
'''
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
length = len(triangle)
columns = len(triangle[length-1])
matrix = [[ 0 for col in range(columns)] for row in range(length)]
row_index = 0
for row in range(length):
elements = triangle[row]
col_index = 0
for val in elements:
matrix[row_index][col_index] = val
col_index += 1
row_index += 1
for row in range(length-2, -1, -1):
for col in range(row+1):
matrix[row][col] += min(matrix[row+1][col+1], matrix[row+1][col])
return matrix[0][0]