Skip to content

Commit 46699e2

Browse files
Update solution.py
1 parent c2f3bf9 commit 46699e2

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

triangle/solution.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ def minimumTotal(self, triangle):
1515
t[row][col] = triangle[row][col] + minsum
1616
row -= 1
1717
return t[0][0]
18+
19+
def minimumTotal2(self, triangle):
20+
if not triangle:
21+
return 0
22+
23+
lastLevel = triangle[-1]
24+
for i in range(len(triangle) - 2, -1, -1):
25+
for j in range(i + 1):
26+
lastLevel[j] = min(lastLevel[j], lastLevel[j + 1]) + triangle[i][j]
27+
28+
return lastLevel[0]

0 commit comments

Comments
 (0)