动态规划
#
# 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
#
#
# @param triangle int整型二维数组
# @return int整型
#
class Solution:
def minTrace(self , triangle: List[List[int]]) -> int:
# write code here
dp = []
for i in range(len(triangle)):
dp.append([0 for _ in range(len(triangle[i]))])
dp[0][0] = triangle[0][0]
for i in range(1, len(triangle)):
for j in range(len(triangle[i])):
if j == 0:
dp[i][j] = dp[i - 1][j] + triangle[i][j]
elif j == len(triangle[i]) - 1:
dp[i][j] = dp[i - 1][j - 1] + triangle[i][j]
else:
dp[i][j] = min(dp[i - 1][j - 1], dp[i - 1][j]) + triangle[i][j]
return min(dp[-1])