LeetCode summary, leetcode
The topic of this article is dynamic planning. It mainly introduces the question of one-dimension dynamic planning in LeetCode. The list is as follows:
Climbing Stairs
Decode Ways
Unique Binary Search Trees
Maximum Subarray
Best Time to Buy and Buy Stock
Before introducing the specific questions above, let's talk about the general idea of dynamic planning. Dynamic Planning is an algorithm IDEA (note that it should not be confused with recursion here. Actually, recursion and iteration are only two different implementation methods, not algorithms). To sum up, dynamic Planning is a method that saves historical information so that historical information does not need to be recalculated in the future, so as to reduce the time complexity and exchange the space complexity for the time complexity. I personally like to divide dynamic planning into the following steps:
1) determine the recurrence. In this step, you need to determine the quantity and specific meaning of historical information to be retained in the recurrence process, and also set the dimension of dynamic planning;
2) Derivation of recursive formula. Based on the specific recurrence quantity, we can obtain the current information result using the stored historical information within the effective time (usually constant or linear time;
3) Calculate the initial condition. With the recursive formula, we only need to calculate the initial condition to obtain the expected result based on the recursive formula. Generally, the initial conditions are relatively simple. Generally, you can assign values directly;
4) (optional) consider the space dimension for storing historical information. This step is based on the consideration of algorithm optimization. Generally, we can use several dimensions of storage space for dynamic planning. However, sometimes we do not have high requirements on historical information. For example, this step only needs to use the historical information of the previous step, instead of having to store the historical information of each step, each step overwrites the information in the previous step, so that one-dimensional storage space can be reduced to optimize the space complexity of the algorithm.
The time complexity of dynamic planning is O (dimension) × (the time complexity used to obtain the current value in each step )). Basically, according to the above ideas, all the questions of dynamic planning can be solved, but the most difficult thing is to determine the recurrence quantity. A good recurrence quantity can minimize the time complexity of dynamic planning.
Next, let's take a look at the specific questions. The one-dimensional dynamic planning questions are mainly divided into two categories:
(1) The first method is relatively simple. It can be solved directly according to the above steps. determine the number of recursion and then obtain it through recursive iteration. This type of question is: Climbing Stairs, Decode Ways, and Unique Binary Search Trees.
The recurrence in Climbing Stairs is clear, that is, the number of feasible crawling methods to climb the I-level Stairs. And for the recurrence we can see that to reach the level I stair, must pass through the I-1 level or the I-2 level (thought can only climb level one or two ), so we can get the way to the level I stair f (I) = f (I-1) + f (I-2), so that the recursive formula will come out. The initial condition is that a level-1 stair is a solution, and two levels of stairs are two solutions (2 or 11 ). With these results, we can return the result by recursion to n-level stairs. The space complexity is O (n) (One-Dimensional Dynamic Planning multiplied by the constant operation of each step ). In space, we find that each step only requires the historical information of the first two steps. Therefore, we do not need to store all the historical information. We only need to save the previous two steps and then iteratively replace them, therefore, the spatial complexity is O (2) = O (1), which corresponds to the fourth step above.
The recursive quantity in Decode Ways is similar to the number that can be parsed to the I character. The recursive quantity is slightly more complex than Climbing Stairs, which should be discussed in detail, we need to process the differences between ourselves and the previous digit. We will not list them here. You can refer to Decode Ways -- LeetCode. Although it is divided into situations, it is possible to update information at constant time in each case. The initial condition is still very simple. In space, you only need to save the information in the first two steps, therefore, the time and space complexity are the same as those of Climbing Stairs.
The concept of Unique Binary Search Trees is similar. The recursive formula is slightly different. It is divided by left and right subtree and then accumulated. Finally, it comes down to the catlan number model. This problem is still one-dimensional dynamic planning, but it is a linear operation to solve the single-step information, so the time complexity is O (n ^ 2 ). In space, because each step requires all the information in the previous step, it cannot be optimized, Which is O (n ).
(2) Next we will introduce the second type. Although it is also a one-dimensional dynamic planning, the difference is that this type of question requires two recursive quantities to be maintained, so there is a bit of thinking skills. However, there are still general methods. I usually refer to this method as "local optimal and global optimal solution". In this method, we usually maintain two quantities. One is the best result information (global optimal) so far, and the other must contain the best result information (local optimal) of the newly added elements ), then we will deduce the recursive formula to calculate the initial conditions, which is the same as the general idea of dynamic planning. Maximum Subarray and Best Time to Buy and Stock are the types of questions.
In Maximum Subarray, we maintain two recursive values: one is the best sub-array so far, and the other is the best sub-array containing the current element after adding the current element, in the end, we look at the optimal value of the global optimal variable, while the local optimum is what we need to maintain the global optimum in the recursive process. Recurrence is a bit tricky. The expression in step I + 1 is as follows:
Local [I + 1] = Math. max (A [I], local [I] + A [I]), that is, the local optimum must contain the current element, otherwise, it is the local optimal local [I] + current element A [I] in the previous step (because local [I] must contain the I element, it does not violate the conditions ), however, if the local [I] is negative, it would be better not to add it as needed. Otherwise, A [I] is used directly;
Global [I + 1] = Math (local [I + 1], global [I]), with the local optimum of the current step, the global optimization is the current local optimization or the original global optimization (all situations will be covered, because if the optimal solution does not contain the current element, it will be maintained in the Global Optimum. if it contains the current element, it is the local optimum ).
The initial conditions are both 0 or the first element can be used, and the two quantities (both constant time) are updated in each iteration, so the time is O (n ). Space shows that only the information in the previous step is needed. Therefore, you only need to save the global optimization and local optimization of the previous step. The complexity is O (2) = O (1 ).
Best Time to Buy and Stock are exactly the same as Maximum Subarray and maintain two quantities. One is the Best transaction so far (the most global ), the other is the best transaction (partial optimization) sold on the previous day. The other steps are the same and will not be listed here.
It can be seen that the above five-dimensional dynamic planning questions are solved according to the four steps listed above. In fact, all dynamic planning questions are based on this basic idea. After mastering the routine, I chose the recurrence quantity to be maintained for specific problems. This personal feeling is still based on experience, which makes perfect practice.
What is leetcode?
There are many programming and interview questions, which can be compiled and run online. It is difficult. If you can do it all by yourself, it is very helpful for large companies. I just did the questions there.
What is blog?
The full name of a blog should be Web log. The Chinese name is "network log", which is abbreviated as Blog, and the Blog (Blogger) is the person who writes the blog. In terms of understanding, a blog is a publishing method that expresses personal thoughts, network links, and content in chronological order ". Simply put, blogs are a type of people who are used to writing diaries online.