描述
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.
分析
终于来了道简单点的找点自信,普通二维动态规划,dp[i][j]
代表以(i, j)
为终点的路径的最小长度,则有dp[i][j] = min(dp[i - 1][j], dp[i][j - 1]) + grid[i][j]
,很容易转化为一维空间。时间O(mn)
,空间O(n)
。
代码
Python
1 | class Solution(object): |