Title:
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.
Ideas:
A typical dynamic programming problem.
DP[I][J] represents the smallest path and from the upper-left corner to Grid[i][j]. Then dp[i][j] = grid[i][j] + min (dp[i-1][j], dp[i][j-1]);
In the following code, in order to deal with the boundary conditions of the first and first columns, we make dp[i][j] represent the smallest path from the upper left to the grid[i-1][j-1] and the last dp[m][n] is the result of our request.
Complexity: O (MN)
Attention:
1. When initializing a vector container, the initial value needs to be set to Int_max, since the smaller values are later to be compared.
VECTOR<VECTOR<INT>>DP (row+1, vector<int> (col+1, Int_max));
2. Need to initialize the value of dp[0][1], dynamic planning needs to set initial values.
3. Note The planned iteration formula, where DP stores a table with a path to go.
DP [M][n] is a matrix store, the min value of every location we can get.
Boundary Processing , Dp[i][j] represents the smallest path and from the upper-left corner to Grid[i-1][j-1].
DP[RI][CI] = grid[ri-1][ci-1] + min (Dp[ri-1][ci], dp[ri][ci-1]);
AC Code:
Class Solution {public: int minpathsum (vector<vector<int> > &grid) { if (grid.empty () | | grid[0 ].empty ()) return 0; int row = Grid.size (); int col = grid[0].size (); Initialized to Int_max, since the minimum value vector<vector<int>>dp (row+1, vector<int> (col+1, Int_max)) is taken back; DP[0][1] requires initialization to start dp[0][1] = 0; for (int ri = 1, ri <= row, ri++) {for (int ci = 1; ci <= col; ci++) { Dp[ri][ci] = grid[ri-1][ci- 1] + min (Dp[ri-1][ci], dp[ri][ci-1]); } } return dp[row][col];} ;
Optimization algorithm: Reduced space complexity to O (min (row, col))
Idea:Dp[i][j] about only the previous row of Dp[i-1][j] and the previous column of Dp[i][j-1], Dp[i][j] is a line of calculation (according to the Loop), Dp[j-1] represents the value of the previous column (has been updated to the value of the previous column of the Bank), Dp[j] The value of the previous row is saved (the DP value is updated line by row, and the value of the row is always stored, which corresponds to the value of the previous row (not yet updated)). Based on this principle, we do not need to store the values of all the columns. Space complexity can be reduced to O (min (row, col))
Attention:
1. When the last value is taken, the last number of the vector container is taken. Dp.back ();
2. Initialize dp[1] = 0; Cycle start dp[1] = grid[0][0] + min (dp[1], dp[0]); <=> dp[1] = grid[0][0] + min (0, Int_max);
AC Code:
Class Solution {public: int minpathsum (vector<vector<int> > &grid) { if (grid.empty () | | grid[0 ].empty ()) return 0; int row = Grid.size (); int col = grid[0].size (); Vector<int> dp (col+1, INT_MAX); DP[1] = 0; for (int ri = 1, ri <= row, ri++) {for (int ci = 1; ci <= col; ci++) { Dp[ci] = grid[ri-1][ci-1] + M In (Dp[ci], dp[ci-1]); } } return Dp.back (); }};
[C + +] leetcode:51 Minimum Path Sum