Note 03 --- dynamic programming algorithm (DP)

Outline

  Dynamic programming, abbreviation: DP 

       Through space for time algorithm ideas        

  To solve a complex problem into sub-problems relatively simple way through the original problem. Usually very similar in many sub-problems, for only trying to solve the problem once for each child dynamic programming method, thereby reducing the amount of calculation: Once a solution to the problem of the stator has been calculated, it is memory storage, so next time you need the same sub-problems when the solution of the direct look-up table.

       Dynamic programming is explained in one sentence, " Remember the things you did before ", if more accurate, in fact, is " to remember the answers you get before ."

        For a dynamic programming problem, we only need to consider from two aspects, that is, to find the link between the problems and record their answers , difficulty here is actually a link between identify problems, record their answers just passing things, the use of Some simple data structure can be done.  

 

Typical application scenarios

  Stairs: Suppose you are climbing stairs. Need n  order to reach your roof. Every time you can climb one or two steps. How many different ways can climb to the roof of it? 

 

 Common problem-solving logic

  • Dismantling problem, find specific links between issues

  • Status Definitions

  • Recurrence equation derivation

  • achieve

  There is in fact focused on the first two, if successfully completed the first two steps, recursive equation derivation and implementation of the code behind will become very simple.

  Or take the example above Quora here to explain, "1 + 1 + 1 + 1 + 1 + 1 + 1 + 1," the answer is eight, then how quickly calculate "1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 ";

  Dismantling problem: we can first be disassembled for this big problem, here I am talking about the big problem is that adding 1 to 9, this problem can be broken down into 1 + "8 1 sum of answers" 8 1 continue adding split, can be broken down into 1 + "answer added 7 1", ... 1 + "answer 0 1 added", here, the first step  has been completed.

  State definition  actually need to think when solving a problem we do anything, then come what kind of answer to this question, the answer to the current problem is the current state, based on the above issue dismantling, you can find two adjacent links is actually a problem , here, every state change is +1. 后一个问题的答案 = 前一个问题的答案 + 1

      Recursive equation: , where the  record is the answer to the current problem, which is the current state of  the record is adjacent to answer before the question of the state is, before the change is implemented state between them by +1. dp[i] = dp[i - 1] + 1dp[i]dp[i - 1]

       Realization : With state representation and recurrence equation to achieve this step important consideration is actually initialized, is what kind of data structure, in accordance with the requirements of issue that needs to be done to set the initial value.

1  public  int dpExample ( int n-) {
 2  
. 3      int [] DP = new new  int [+ n-1];   // to open a 0 1 for storing the result of addition 
. 4  
. 5      DP [0] = 0;       / / 0 1 add up to 0 
. 6  
. 7      for ( int I = 1; I <= n-; ++ I) {
 . 8          DP [I] DP = [I - 1] + 1 ; // recursive equation 
 9      }
 10  
. 11      return DP [n-];
 12 is }
 

Title: 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: Output 2: 2
explanation: There are two methods can climb the roof.
           Method 1) 1 + 1-order-order
           Method 2) Order 2

Example 2: Input: Output 3: 3
Explanation: There are three methods can climb to the roof.
           1) Order 1 + 1 + 1-order-order
           Method 2) + 1-order-order 2
           Method 3) 2 + 1-order-order

Analysis process

Climbing stairs, you can climb one step can also climb two steps, I asked how many different ways to reach the end, we analyzed in accordance with the above-mentioned four steps:

  • Dismantling question :

    We can reach the n-th stair from the n - 2 reaches the stairs, so the n-th problem can be broken down into the n - - and the stairs. 1 n 1 n and the problem - two problems, the first n - 1 questions and n - 2 questions and can continue to split down until the first 0 issue, which is the first 0 stairs (starting point)

  • Status Definitions

    "The problem dismantling" already mentioned, the n-th and stairs will be the first n - 1 and n - 2 staircases linked, what is it that specific contact? In this way you can think of n - a question the answer to which is actually from the starting point to reach the first n - total number of paths a staircase, n - 2 Similarly, from the first n - 1 stairs to reach the n-th stairs, from the first n - 2 can, and did not repeat the path, so we can put "i-th state is defined as reaching the stairs of the i-th path from the starting point of the total number of " links between the state is actually the sum relationship.

  • Recurrence equation

    "Status Definitions" We have defined the state, but also know the i-th state may by the first i - 1 states and the i - is obtained by adding two status, recurrence equation therefore came out dp[i] = dp[i - 1] + dp[i - 2]

  • achieve

    In fact, you can see from the recurrence equation, we have a need to facilitate initial value we calculated, without moving the start position , the stairs can only reach the first layer from a starting position, and therefore , the second layer 2 starting from the stairs and the position of the first layer stairs, therefore , with the initial values, the latter can be obtained by these recursive initial value. dp[0] = 0 dp[1] = 1 dp[2] = 2

The sample code

. 1  public  int climbStairs ( int n-) {
 2  
. 3      IF (n-==. 1 ) {
 . 4          return . 1 ;
 . 5      }
 . 6  
. 7      int [] DP = new new  int [n-+. 1];   // to open a considered starting position 
. 8  
. 9      DP [0] = 0; DP [. 1] =. 1; DP [2] = 2 ;
 10  
. 11      for ( int I =. 3; I <= n-; ++ I) {
 12 is          DP [I] DP = [I -. 1] + DP [I - 2 ];
 13 is      }
 14  
15      return DP [n-];
16 }

 

 

  

Guess you like

Origin www.cnblogs.com/clarino/p/11965089.html