题目
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.
Example:
1 | Input: |
思路
DP.
新的一个点的最短路径一定等于其上方、左方最短路径+当前的值,
即有:grid[i][j] = min(grid[i-1][j], grid[i][j-1]) + grid[i][j]
同时注意i=0和j=0时的边界。
代码
1 | class Solution(object): |