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.
Hide TagsArray Dynamic ProgrammingIdeas: Similar to unique path, Uique path2, are dynamic programming, note that the initial value here is Int_max
classSolution { Public: intMinpathsum (vector<vector<int> > &grid) { intm =grid.size (); intn = grid[0].size (); Vector<int> Row (n +1, Int_max); Vector<vector<int> > F (M +1, Row); #if0 for(inti =0; I < m; i + +) { for(intj =0; J < N; J + +) {cout<< Grid[i][j] <<"\ t"; } cout<<Endl; } cout<<Endl; #endiff[1][1] = grid[0][0]; for(inti =1; I <= m; i + +) { for(intj =1; J <= N; J + +) { if(i = =1&& J = =1) Continue; F[I][J]= Min (f[i-1][J], f[i][j-1]) + grid[i-1][j-1]; } } #if0 for(inti =1; I <= m; i + +) { for(intj =1; J <= N; J + +) {cout<< F[i][j] <<"\ t"; } cout<<Endl; } #endif returnF[m][n]; }};
[Leetcode] Minimum Path Sum