LeetCode Brush title - Basics articles - Dynamic Programming

  Record about "Introduction to Algorithms" in some of the knowledge about the dynamic programming as well as their own ideas.

Dynamic Programming

  It is a dynamic programming algorithm to solve the original problem through a combination of sub-problems. Dynamic programming is applied to the case of overlapping sub-problems, i.e., have a common problem of different sub-sub-sub-problems (subproblems is performed recursively, it will be divided into smaller sub-sub-problem). Are recalculated this case, the dynamic programming algorithm for each sub-sub-problem solving only once, and its solution is stored in a table, eliminating the need for every child to solve a problem child, to avoid unnecessary calculations. Dynamic programming is typically used to solve optimization problems.

  Design a dynamic programming algorithm can generally be divided into four steps

  1. Characterize an optimal solution value
  2. Recursive definition of the value of the optimal solution
  3. Calculating optimum values, usually the bottom-up method
  4. Using the calculated information to construct an optimal solution

  Steps 1 to 3 is the basis of dynamic programming algorithm to solve the problem. If we only need the value of an optimal solution, not the solution itself, you can omit step 4. If you do need to step 4, and sometimes need to maintain some additional information in the course of step 3 in order to build an optimal solution .

Dynamic programming principle

  The aforementioned dynamic programming is often used to solve "the optimization problem," then what kind of specific questions for the use of dynamic programming to solve it? Dynamic programming method for solving optimization problems should have two elements: optimal substructure and sub-issues overlap .

Optimal substructure:

  The first step in solving the optimization problem of dynamic programming is to characterize the structure of the optimal solution. If the optimal solution contains optimal solution to a problem of his son's problems, we call this problem has structural sub-optimal nature. But therefore, we must be careful to ensure that examines all the sub-optimal solution used in. In the process of realizing the optimum properties of the substructure in fact follow the general pattern of the following:

  1. The first component part proven optimal solution is to make a choice, the choice will produce one or more sub-problems to be solved.
  2. For a given problem, the first step in the possible choices, assume that you already know which option will get the optimal solution. You do not care how this choice is to get specific, but assumed already know this choice.
  3. Given available to choose optimal solutions, are you sure this problem will have to choose which child, and how best to describe the problem of sub-space.
  4. As part of the original problem constitutes the optimal solution, the solution of the problem is that each sub-optimal solution of its own.

Overlapping sub-problems:

  Apply dynamic programming method for solving optimization problems should have a second property is a sub-issue of space must be sufficiently "small", that is, a recursive algorithm to solve the problem will be repeated the same sub-problems, but not always generate a new sub-problems. In general, the total number of different sub-problem is a polynomial function of the input size as well. If repeated recursive algorithm to solve the same problem child, we say optimization problem has overlapping subproblems nature.

 

Some can be solved using dynamic programming topics:

Cutting steel bars:

  This question should be the most classic title of dynamic planning, the question is this: a company buys long steel bars, steel cut it short advertisements. Cutting process itself is not costs. The company's management would like to know the best cutting program. Suppose we know the sale price for a length of steel bars i inch of pi, the length of the steel bars are full inch. Give a sample price list as follows:

Length i 1 2 3  4 5 6 7 8 9 10
Price pi 1 5 8 9 10 17 17 20 24 30

 

  Given a length of steel bar n and a price list, a cutting program so that the sales proceeds seeking maximized. Note that if the length of n-inch steel bar prices pn is large enough, then the best solution is probably completely without cutting.

  We can use a thought to ponder this question: After completion of the first cut will be seen as two separate steel bars at both ends of the cutting issues Examples of steel bars. We optimum combination of these two sub-problems related to, and select all possible two schemes benefit most from the cutting, to constitute our original problem ----- cut length of steel strip optimum n solution. I.e. steel rods of length n The maximum benefits of:

    Rn = Max(Pn, R1+R(n-1), R2+R(n-2) ...... , R(n-1) + R1)

  The above equation in Rn representative length of maximum benefit steel bars n, such as R2 + R (n-2), which represents a length of steel strip 2. Maximum benefit length of maximum benefit steel strip n-2 and. And we get a length of steel bar n the way to the maximum benefit of all possible two- segment selection largest beneficiaries cutting programs. I.e., maximum value of the series combination of the optimal solution.

  Using dynamic programming method to solve the problem of optimal cutting steel bars in two ways.

Top-down method with notes:

  This method is based upon the natural form of recursive writing process, but the process will be the solution for each sub-problem of preservation. When you need a sub-problem solution process first cut whether this solution has already been saved, if the return is directly saved values, thus saving computing time. This pseudo-code is given below of the method:

 1 MEMOIZED-CUT-ROD(p, n)
 2 {
 3     let r[0...n] be a new array
 4     
 5     for(int i = 0; i <= n; i++)
 6     {
 7         r[i] = -∞;
 8     }
 9     
10     return MEMOIZED-CUT-ROD-AUX(p,n,r)
11 }
12 
13 MEMOIZED-CUT-ROD-AUX(p,n,r)
14 {
15     if(r[n] >= 0)
16     {
17         return r[n];
18     }
19     
20     if(n == 0)
21     {
22         q = 0;
23     }
24     else
25     {
26         q = -∞;
27         
28         for(int i = 1; i<= n; i++)
29         {
30             q = MAX(q, p[i] + MEMOIZED-CUT-ROD-AUX(p,n-i,r))
31         }
32     }
33     
34     r[n] = q;
35     
36     return q;
37 }

  Here the first auxiliary array r [0 ... n] is initialized to element -∞. In fact, here is the value corresponding to this marked as "unknown" because we always determine the income of non-negative, and therefore such a value can be determined. It has not been calculated. In MEMOIZED-CUT-ROD-AUX ( p, n, r) during the first check whether the desired value is known, if known directly returned, otherwise the desired value is calculated, and then recorded. Note 28 to 31 lines, in fact, thinking we mentioned before, selecting the largest beneficiaries in all possible two-stage cutting programs.

 

Bottom-up approach:

  This method usually requires an appropriate concept of "scale" is defined as sub-problems, so that any child solving problems only depend on the "smaller" sub-problem solving. So we can sub-problems are ordered by size, by ascending order to solve it. When solving a problem child, those smaller sub-problems have been solved it depends completed, the results have been saved. Each sub-problem solving only once, when we solve it, all its premise sub-problems are solved completed. The following pseudo-code is given in this manner:

 1 BOTTOM-UP-CUT-ROD(p,n)
 2 {
 3     let r[0...n] be a new array
 4     r[0] = 0;
 5     
 6     for(int j = 1; j <= n; j ++)
 7     {
 8         q = -∞;
 9         
10         for(i = 1; i <= j; i++)
11         {
12             q = max(q, p[i] + r[j-i]);
13         }
14         
15         r[j] = q;
16     }
17     
18     return r[n];
19 }

  This method uses a natural sequence of sub-problems: if i <j, i is the scale ratio for the sub-problems scale subproblems j "is smaller." Therefore, in order to solve.

  Another: There when I first saw here a doubt that the manner described above corresponds to the length of the table and the prices given us This question, what would happen if the length is greater than 10? p [i] is the index will be reported error beyond the border. Indeed, to the bottom-up approach of the practical application of the above, there is also the need for additional logic part, it is to update the p [] array, in order to ensure that you can still use when they encounter problems "bigger" scale has been seeking out of the "smaller" scale solution.

 

Attach the following two topics LeetCode dynamic programming:

  Source: stay button (LeetCode), portal

53. The maximum and subsequence

Given the nums an array of integers, and find a maximum of the successive sub-array (sub-array comprising a minimum element) having its maximum and returns.

Example:

Input: [-2,1, -3,4, -1,2,1, -5,4],
output: 6
Explanation: continuous subarray [4, -1,2,1], and the maximum was 6 .

 1 public class Solution {
 2         public int MaxSubArray(int[] nums)
 3         {
 4             int maxValue = nums[0];
 5 
 6             for (int i = 1; i < nums.Count(); i++)
 7             {
 8                 if (nums[i - 1] > 0)
 9                 {
10                     nums[i] += nums[i - 1];
11                 }
12 
13                 maxValue = Math.Max(maxValue, nums[i]);
14             }
15 
16             return maxValue;
17         }
18 }

  This question actually need to think of a point to understand, once figured out you can understand the above code. I.e., the above code is in fact determined largest subtree of the array nums group at each position and that can be obtained. In other words, such index is 0, then the maximum, and at this position of nums [0]. Another example index is 3, the maximum and that Max (nums [0] + nums [1] + nums [2] + nums [3], nums [1] + nums [2] + nums [3], nums [2 ] + nums [3], nums [3]).

  With the idea of dynamic programming to explain that the maximum value of each position can be achieved and in fact the value of its own as well as the largest and the value of its previous position that can be achieved closely related.

 

70. stairs

Suppose you are climbing stairs. N order you need to get to the roof.

Every time you can climb one or two steps. How many different ways can climb to the roof of it?

Note: Given n is a positive integer.

Example 1:

Input: 2
Output: 2
explanation: There are two methods can climb the roof.
1.1 + 1-order-order
2.2 Order
Example 2:

Input: 3
Output: 3
Explanation: There are three methods can climb to the roof.
1.1 + 1 order + 1-order-order
2.1 + 2-order order
3.2 + 1-order-order

 1 public class Solution {
 2         public int ClimbStairs(int n)
 3         {
 4             if (n == 0)
 5             {
 6                 return 0;
 7             }
 8 
 9             if (n == 1)
10             {
11                 return 1;
12             }
13 
14             var n_Count = new int[n];
15 
16             n_Count[0] = 1;
17             n_Count[1] = 2;
18 
19             for (int i = 3; i <= n; i++)
20             {
21                 n_Count[i - 1] = n_Count[i - 2] + n_Count[i - 3];
22             }
23 
24             return n_Count[n_Count.Length - 1];
25         }
26 }

 

  This question of the idea is, in fact, to reach this level n there are two ways, i.e. an order from the crawl n-1, or to climb from the two-step n-2. Thus it reaches the n-layer and in all methods is to all methods of layer n-1 and n-2 + reach all of the layers and methods .

 

  Dynamic programming still has a lot of interesting topics, such as the longest common subsequence, matrix multiplication ah, etc., is not limited by the length of the record.

Guess you like

Origin www.cnblogs.com/dogtwo0214/p/12112732.html