Dynamic programming is a very important algorithm design idea. Historically there have been many well-known algorithms based on this idea, such as: Needleman–wunsch algorithm, CYK algorithm, FFT algorithm, Viterbi algorithm and so on. There are two core ideas in dynamic programming: First, to disassemble a large problem into several sub problems, and then to store the results that have been calculated for multiple use.
Dynamic planning is important because it is a frequent focus of written interview topics for various IT companies, and it is a prerequisite for all players wishing to compete in the informatics contest. But dynamic programming for many beginners, is relatively difficult to master a skill, leetcode dynamic programming topics mostly belong to median and hard degree. But generally speaking, the general dynamic programming problem has the routine, through grasps this kind of routine, actually may solve quite part of the dynamic programming problem.
The following two Leetcode topics for further explanation. The first is the unique paths problem numbered #62, which is described as follows:
The idea is to give a matrix that asks you how many different paths there are in the upper-left corner and the lower-right corner. The only way to move robot is to either move down one grid or move one grid to the right.
The first step in solving a dynamic programming problem is to write a recursive solution of a "preliminary". For the current problem, you want to move to the grid [M,n], either from [M,n-1] (that is, one of the left), or from [M-1,n] (that is, the top of the grid). So we know that the number of paths to the grid [M,n] is the number of paths to the grid [m,n-1] + the number of paths to the grid [M-1,n], which is the most central recursive relationship (usually in the dynamic programming problem we call the state transition equation). Then, for a recursive problem, you also need to consider some base condition to ensure that your recursive decomposition to the atomic problem can be returned correctly. Based on the above ideas, let's first write a recursive version (note that dynamic planning is not used):
Class Solution {public
:
int uniquepaths (int m, int n) {
if (m==1 && n==1) return 1;
if (m==1) return
uniquepaths (M, n-1);
if (n==1) return
uniquepaths (m-1, n);
Return (Uniquepaths (m-1, N) + uniquepaths (M, n-1));
}
;
However, the above version submits the Leetcode directly, and the timeout is displayed. Because there will be a lot of duplicate computations. To do this, you need to use the second step in a routine that solves a dynamic programming problem: Define an array or matrix to store the results that have been obtained. When you need to use them again, check the table directly without having to recalculate.
As far as the current problem is concerned, it may be useful to define a matrix. Because the title has been stated: The maximum value of M and N is 100, and the initial position of the map is [1,1], so we define the matrix size of 101x101, of course, the leftmost column and the top row will not be used. Then we initialize the entire matrix, each position is-1, indicating that the position has not been computed. The position of [1,1] is then initialized to 1, which means that if the map has only one lattice, then there are only 1 lines. The resulting code is as follows:
Class Solution {public
:
int matrix[101][101];
Solution () {for
(int i = 0; i<101; i++) for
(int j = 0; j<101; j + +)
Matrix[i][j] =-1;
MATRIX[1][1] = 1;
}
int uniquepaths (int m, int n) {
if (Matrix[m][n]!=-1) return
matrix[m][n];
if (m==1)
{
Matrix[m][n] = uniquepaths (M, n-1);
return matrix[m][n];
}
if (n==1)
{
Matrix[m][n] = uniquepaths (m-1, n);
return matrix[m][n];
}
Matrix[m][n] = uniquepaths (m-1, N) + uniquepaths (m, n-1);
Return (Matrix[m][n]);
}
;
The above implementation can meet the requirements of the topic. Of course, because for demonstration purposes, and to optimize for it, you can also improve it to achieve higher efficiency.
Another variant is provided in the Leetcode, which is numbered #63. Given a matrix in initial conditions to represent a map, where a value of 1 represents an obstruction. Then assume that there are obstacles and ask how many different paths you have.
In fact, if you can solve the #62 problem, then this problem is actually very simple. We mainly add a base condition in the recursive process, that is, if the target point is an obstacle, then the path to it is 0. The code based on this small change is as follows:
Class Solution {public:int matrix[101][101]; Solution () {for (int i = 0; i<101; i++) for (int j = 0; j<101; j + +) Matrix[i][j] =
-1; int uniquepaths (int m, int n, vector<vector<int>>& obstaclegrid) {if (matrix[
M][n]!=-1) return matrix[m][n];
if (obstaclegrid[m-1][n-1] = = 1) {Matrix[m][n] = 0;
return 0;
} if (m==1) {Matrix[m][n] = uniquepaths (M, n-1, Obstaclegrid);
return matrix[m][n];
} if (n==1) {Matrix[m][n] = uniquepaths (m-1, N, Obstaclegrid);
return matrix[m][n];
} Matrix[m][n] = Uniquepaths (m-1, N, Obstaclegrid) + uniquepaths (M, n-1, Obstaclegrid);
Return (Matrix[m][n]); int Uniquepathswithobstacles (vector<vector<int>>& ObstacLegrid) {int m = obstaclegrid.size ();
int n = obstaclegrid[0].size (); matrix[1][1]= obstaclegrid[0][0]==1?
0:1;
Return Uniquepaths (M, N, Obstaclegrid); }
};
The only thing to note is that the new matrix provided as a parameter starts with [0,0] (not starting with [1,1]). So there will be a little bit of a change in initialization.
(End of full text)
List of leetcode topics already discussed in this blog
two dynamic programming topics in Leetcode (#62, #63) dynamic Programming topics in Leetcode (2) (#64) Dynamic programming Problem solving in Leetcode (3) (#72, #718) max Continuous subsequence and problem (#53) See if you really mastered binary Search (#35) zigzag arrangement problem and analysis of classic written interview (#6) the problem of bracket matching and the analysis of classic written interview questions (#20, #32) Newton iterative method and a classical programming problem (#69) Three tricky Leetcode interview topic analysis (#48, #169, #231) Leetcode topic resolution on hashing (#187, #389) Yang Hui's triangle and a classic written interview topic (#118, #119) Polish expression (Reverse Polish notation) (#150)