-
Notifications
You must be signed in to change notification settings - Fork 0
/
120.py
24 lines (22 loc) · 846 Bytes
/
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
class Solution(object):
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
if not triangle:
return 0
for i in range(len(triangle)):
for j in range(len(triangle[i])):
if len(triangle[i]) == 1:
triangle[i][j] = triangle[i][j]
else:
if j == 0:
triangle[i][j] = triangle[i - 1][j] + triangle[i][j]
elif j == len(triangle[i]) - 1:
triangle[i][j] = triangle[i - 1][-1] + triangle[i][j]
else:
triangle[i][j] = min(triangle[i - 1][j - 1], triangle[i - 1][j]) + triangle[i][j]
return min(triangle[-1])
solu = Solution()
print solu.minimumTotal([[2]])