首页 > 代码库 > Leetcode#64 Minimum Path Sum

Leetcode#64 Minimum Path Sum

原题地址

 

基本动态规划题

 

代码:

 1 int minPathSum(vector<vector<int> > &grid) { 2   if (grid.empty() || grid[0].empty()) return 0; 3  4   int m = grid.size(); 5   int n = grid[0].size(); 6   vector<int> sum(n, 0); 7  8   sum[n - 1] = grid[m - 1][n - 1]; 9   for (int j = n - 2; j >= 0; j--)10     sum[j] = grid[m - 1][j] + sum[j + 1];11 12   for (int i = m - 2; i >= 0; i--) {13     sum[n - 1] += grid[i][n - 1];14     for (int j = n - 2; j >= 0; j--)15       sum[j] = min(sum[j], sum[j + 1]) + grid[i][j];16   }17 18   return sum[0];19 }

 

Leetcode#64 Minimum Path Sum