forked from AnasImloul/Leetcode-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTriangle.py
More file actions
21 lines (20 loc) · 696 Bytes
/
Triangle.py
File metadata and controls
21 lines (20 loc) · 696 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Solution:
def minimumTotal(self, t: List[List[int]]) -> int:
dp = []
dp.append(t[0])
r = len(t)
answer = float('inf')
for i in range(1, r):
c = len(t[i])
dp.append([])
for j in range(0, c):
if j == 0:
val = dp[i - 1][j] + t[i][j]
elif j == c - 1:
val = dp[i - 1][j - 1] + t[i][j]
else:
val = min(dp[i - 1][j], dp[i - 1][j - 1]) + t[i][j]
if i == r - 1:
answer = min(answer, val)
dp[i].append(val)
return answer if r > 1 else t[0][0]