class Solution: # @param triangle, a list of lists of integers # @return an integer def minimumTotal(self, triangle): n=len(triangle) if n==0:return 0 sv=[triangle[0][0]] for i in range(1,n):# given sv for row i, calculate sv for row i+1 new_sum=[0 for x in range(i+1)] for j in range(i+1): if j==0: new_sum[0]=triangle[i][0]+sv[0] elif j==i: new_sum[i]=triangle[i][i]+sv[i-1] else: new_sum[j]=min(triangle[i][j]+sv[j-1],triangle[i][j]+sv[j]) sv=new_sum[:] return min(sv)
Friday, September 12, 2014
Leetcode: Triangle @Python
Subscribe to:
Post Comments
(
Atom
)
No comments :
Post a Comment