Minimum Path Sum
Given a m x n grid filled with non-negative numbers, find a path from top left to bottom right which minimizes
the sum of all numbers along its path.
Note: You can only move either down or right at any point in time.
题意
注意只能往下和往右走
思路
简单难度,先初始化边缘,然后“步步为营”
python实现
class Solution:
def minPathSum(self, grid):
m = len(grid)
n = len(grid[0])
dp = [[0] * n for _ in range(m)]
dp[0][0] = grid[0][0]
# 初始化边缘
for i in range(1, n):
dp[0][i] = dp[0][i - 1] + grid[0][i]
for i in range(1, m):
dp[i][0] = dp[i - 1][0] + grid[i][0]
for i in range(1, m):
for j in range(1, n):
dp[i][j] = min(dp[i-1][j] + grid[i][j], dp[i][j-1] + grid[i][j])
return dp[m-1][n-1]
if __name__ == '__main__':
so = Solution()
a = [[1, 3, 5, 7], [10, 11, 16, 20], [23, 30, 34, 50]]
print(so.minPathSum(a))