class Solution:
def minimumTotal(self, triangle):
"""
:type triangle: List[List[int]]
:rtype: int
"""
# dp
if not triangle:
return 0
R = C = len(triangle)
dp = [[0] * C for _ in range(R)]
# 初始值
dp[0][0] = triangle[0][0]
# 状态转移方程
for i in range(1, R):
for j in range(i + 1):
if j == 0:
dp[i][j] = dp[i - 1][j] + triangle[i][j]
elif i == j:
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])