题目
思路
dp,滚动数组
代码
class Solution:
"""
@param grid: a list of lists of integers
@return: An integer, minimizes the sum of all numbers along its path
"""
def minPathSum(self, grid):
# write your code here
dp = [i for i in grid[0]]
for i in range(len(dp)):
if i: dp[i] = grid[0][i] + dp[i - 1]
for i in range(1, len(grid)):
for j in range(len(grid[0])):
if j == 0: dp[j] = grid[i][0] + dp[0]
else: dp[j] = grid[i][j] + min(dp[j - 1], dp[j])
return dp[len(grid[0]) - 1]