标签:
这道题纯dp, 但是有一个比一下代码优化的做法,就是从底部bottomu-up dp.
class Solution: # @param triangle, a list of lists of integers # @return an integer def minimumTotal(self, triangle): n = len(triangle) ans = [[0] * n for i in range(0,n)] if n == 0: return 0 ans[0][0] = triangle[0][0] for i in range(1, n): ans[i][0] = ans[i-1][0] + triangle[i][0] ans[i][i] = ans[i-1][i-1] + triangle[i][i] for j in range(1,i): ans[i][j] = min(ans[i-1][j-1], ans[i-1][j]) + triangle[i][j] return min(ans[-1])
标签:
原文地址:http://www.cnblogs.com/dapanshe/p/4644325.html