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.
Example 1
1 | Input: |
解题思路
动态规划:要到达位置 [m,n],必须先到达位置 [m,n-1] 或 [m-1,n],其最小路径和为到达 [m,n-1] 或 [m-1,n] 的最小值加上位置 [m,n] 的数值。
复杂度分析
- 时间复杂度:$O(mn)$。
- 空间复杂度:$O(1)$,可直接利用
grid。
代码
1 | class Solution { |