Skip to content

Commit fbf3da5

Browse files
Create triangle.py
LeetCode ques 120, utilizing DP
1 parent a71618f commit fbf3da5

1 file changed

Lines changed: 11 additions & 0 deletions

File tree

dynamic_programming/triangle.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class Solution:
2+
def minimumTotal(self, triangle: List[List[int]]) -> int:
3+
for row in range(1, len(triangle)):
4+
for col in range(row + 1):
5+
smallest_above = math.inf
6+
if col > 0:
7+
smallest_above = triangle[row - 1][col - 1]
8+
if col < row:
9+
smallest_above = min(smallest_above, triangle[row - 1][col])
10+
triangle[row][col] += smallest_above
11+
return min(triangle[-1])

0 commit comments

Comments
 (0)