Question
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.
本题难度Medium。
【复杂度】
时间 O(MN) 空间 O(MN)
【思路】
与[LeetCode]Unique Paths差不多。用DP方法,遍历整个矩阵,对于每个点dp[i][j]
,从dp[i-1][j]
和dp[i][j-1]
中选取较小值,与grid[i][j]
相加得到该点最小值。
【注意】
7-11行为初始化dp
的边界值。
【代码】
参考
[LintCode/LeetCode] Minimum Path Sum