LeetCode:64. Minimum Path Sum(Week 4)
阿新 • • 發佈:2018-11-10
64. 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:
Input: [ [1,3,1], [1,5,1], [4,2,1] ] Output: 7 Explanation: Because the path 1→3→1→1→1 minimizes the sum.
-
-
解題思路
- 本題比較簡單,題意是尋找m x n格中左上角到右下角路徑的最小距離,並且沒有負權值
dp[i][j]:起點到點 [i,j]的最小距離
- 因為沒有負路徑和尋路方向只能向下或者向右,因此對於每個點(除起點),其
dp[i][j]
的表示為dp[i][j] = min(dp[i-1][j],dp[i][j-1]) + grid[i][j]
-
實現程式碼
class Solution { public: int minPathSum(vector<vector<int> >&