Feel this is a series of dynamic programming algorithms, just as well as the dynamic programming algorithm to make a summary:
Algorithm one:
The problem of least path with weights
Given amxNgrid filled with non-negative numbers, find a path from top left to bottom right whichminimizesThe sum of all numbers along its path.
First, the previous path of each path is from above and to the left.
Now the topmost path is summed, the leftmost path is summed (the reason why there is no direct summation is because of a one-dimensional array res[n] record path, here is more ingenious to save space, so the leftmost summation algorithm put in the loop inside res[i][j] = min (res[i-1][ J],RES[I][J-1]) +val[i][j])
Class Solution {public: int minpathsum (vector<vector<int> > &grid) { if (grid.empty () | | grid[0 ].empty ()) { return 0; } int m = Grid.size (); int n = grid[0].size (); int *res = new Int[n]; Res[0] = grid[0][0]; for (int i = 1;i < n; i++) { res[i] = Res[i-1]+grid[0][i]; } for (int i = 1, i < m; i++) {for (int j = 0; J < N; j + +) { if (0 = = j) { Res[j] + = Grid I [0]; } else { Res[j] = min (res[j-1],res[j]) +grid[i][j];}} } int result = Res[n-1]; delete []res; return result;} ;
Similar to this dynamic programming algorithm is:
Unique Paths
https://leetcode.com/problems/unique-paths/
A robot is located at the Top-left corner of a m x n grid (marked ' Start ' in the diagram below).
The robot can only move either down or right at any point in time. The robot is trying-to-reach the bottom-right corner of the grid (marked ' Finish ' in the diagram below).
How many possible unique paths is there?
Above is a 3 x 7 grid. How many possible unique paths is there?
Note: m and N would be is at most 100.
Class Solution {public: int uniquepaths (int m, int n) { if (m = = 0 | | n = = 0) { return 0; } int res[100][100]; for (int i = 0; i < n; i++) { res[0][i] = 1;//< because there is only one method } for (int i = 0; i < m; i++) {
res[i][0] = 1; } for (int i = 1, i < m; i++) {for (int j = 1; j < N; j + +) { Res[i][j] = res[i-1][j]+res[i][j-1];
} } return res[m-1][n-1];} ;
If you are using only one-dimensional space:
Class Solution {public: int uniquepaths (int m, int n) { if (M <= 0 | | n <= 0 { return 0; } int res[100] = {0}; Res[0] = 1; for (int i = 0, i < m; i++) {for (int j = 1; j < N; j + +) { Res[j] + = res[j-1]; } } return res[n-1];} ;
Unique Paths II
Here are some obstacles, if the value of 1 means there are obstacles, can not cross
[ [0,0,0], [0,1,0], [0,0,0]
The total number of unique paths is 2
.
Note: m andN'll is at the most .
Class Solution {Public:int uniquepathswithobstacles (vector<vector<int> > &obstaclegrid) {if (ob Staclegrid.empty () | | Obstaclegrid[0].empty ()) {return 0; } int res[100] = {0};//< or one-dimensional array int m = Obstaclegrid.size (); int n = obstaclegrid[0].size (); Res[0] = (obstaclegrid[0][0]! = 1); for (int i = 0, i < m; i++) {for (int j = 0; J < N; j + +) {<span style = "COLOR: #ff0000;" >if (J = = 0) {if (obstaclegrid[i][j] = = 1)//< Here's a little bit of attention. If the current barrier is represented as 0, you can continue the previous state {Res[j] = 0; }}</span> else {<span style= "color: #ff0000;" >if (Obstaclegrid[i][j] = = 1) {Res[j] = 0; } else { RES[J] + = res[j-1]; }</span>}}} return res[n-1]; }};
Leetcode-minimum path Sum Two-dimensional array minimum path, dynamic programming