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.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 class Solution { 2 public: 3 int minPathSum(vector<vector<int>>& grid) { 4 int m=grid.size(); 5 if(!m) 6 return 0; 7 int n=grid[0].size(); 8 9 vector<int> jlist(n,0); 10 int t=0; 11 for(int i=n-1;i>=0;i--) 12 { 13 jlist[i]=grid[m-1][i]+t; 14 t=jlist[i]; 15 } 16 17 for(int i=m-2;i>=0;i--) 18 { 19 for(int j=n-1;j>=0;j--) 20 { 21 int temp; 22 if(j==n-1) 23 { 24 temp=jlist[j]; 25 } 26 else 27 { 28 temp=jlist[j]>jlist[j+1]?jlist[j+1]:jlist[j]; 29 } 30 jlist[j]=grid[i][j]+temp; 31 } 32 } 33 return jlist[0]; 34 35 } 36 };