A typical dynamic programming topic summary (Fibonacci-related columns)

1. Conventional jump stairs

Once a frog can hop on level 1 level, you can also hop on level 2. The frog jumped seeking a total of n grade level how many jumps (the order of different calculation different results).

General idea:

To reach the first number of i-1 and i-2 of the stairs of the i-th method of stairs from the first step to reach i-1 and i-2 walk stairs, went i.e. the i-th number of stairs and methods . Recursion formulas can be derived as: DP [I] DP = [-I. 1] + DP [I-2]

Considering the dp [i] with only dp [i - 1] and dp [i - 2], and therefore can only two variables to store dp [i - 1] and dp [i - 2], so that the original O (N) is optimized for space complexity (1) complexity of O. Specific non-recursive code is as follows:

 

 

 2. metamorphosis jump stairs

Once a frog can hop on level 1 level, you can also hop on level 2 ...... n It can also jump on stage . The frog jumped seeking a total of n grade level how many jumps.

The general idea:

(1) the height of each step can be completed in one step, so that each location has a basic solution dp [i] = 1

(2) In addition to the basic solution, the height of each of a complete solution and is also subject to the influence of an adjacent highly dp [i-1] before being only.

(3) Accordingly, the height of each of the final solution is a solution of the number of fundamental solution itself before + (). I.e., DP [I]. 1 + DP = [. 1-I];

 3. robbed a row of houses

You are a professional thief to steal street plan of the house. Each room are in possession of some cash, the only constraints affecting you steal is adjacent to the house is equipped with anti-theft system communicate with each other , if two adjacent houses on the same night, thieves broke into the system will automatically alarm . Given a representative from each non-negative integer array of Housing storage amount calculated in case you do not touch the alarm device, you can steal the maximum amount to.

General idea:

(1) == adjacent rooms can not steal "steal can not steal i-1 i.
(2) then if i is a last row of a room, then the maximum value of the current i through the rooms of :
  DP [I] = max (DP [I-2] + numms [I], DP [-I. 1]);
therefore, it requires two values dp [i-1], dp [i-2].

class Solution {
    public int rob(int[] nums) {
        int pre1=0;
        int pre2=0;
        for(int i=0;i<nums.length;i++){
            int temp=Math.max(nums[i]+pre2,pre1);
            pre2=pre1;
            pre1=temp;
        }
        
        return pre1;
    }
}

4. Robbery around the house (the ring of the first and the last one is adjacent)

You are a professional thief to steal street plan of the house, each room are in possession of some cash. This place all the houses are in a circle , which means that the first and the last house is next to the house . Meanwhile, neighboring houses equipped with anti-theft system communicate with each other, if two adjacent houses on the same night, thieves broke into the system will automatically alarm. Given a representative from each non-negative integer array of Housing storage amount calculated in case you do not touch the alarm device, you can steal the maximum amount to.

Problem-solving ideas:

/ **
(1) consider the linear case, when a total room i, i stolen, no longer steal i-1. Steal only i-2.
Therefore dp [i] = Math.max ( the nums [I] + DP [I-2], DP [-I. 1]);
(2) an annular channel, it can be considered on the basis of linear, because the first and last become adjacent, so
We can not steal first stole the last one.
The second began to steal from, before they can steal the last one.
Therefore, from both a maximum path selected as the final result value.
** /

class Solution {
    public int rob(int[] nums) {
        //1.特殊值判断
        if(nums.length==1){
            return nums[0];
        }
//环形解
        int fromOne=maxVal(nums,0,nums.length-2);
        int fromTwo=maxVal(nums,1,nums.length-1);
        return Math.max(fromOne,fromTwo);
    }
    
//直线型解
    private int maxVal(int[]nums,int start,int end){
        int pre1=0;
        int pre2=0;
        for(int i=start;i<=end;i++){
            int temp=Math.max(nums[i]+pre2,pre1);
            pre2=pre1;
            pre1=temp;
        }
        
        return pre1;
    }
}

5. Daniel calving problems

Assuming that farm mature cows every year a heifer born and will never die . The first year a heifer, from the second year, students begin cow heifers. Each heifer mature after three years and may be born heifers. Given an integer N, the number of cattle seeking N years later.

Ideas:

(1) the first three years, only a cow can produce.

(2) because all the cattle will not die, therefore, the first N-1年的所有牛 dp [N-1] will live to N years.

  (3) because the mature cows born every year a heifer. Therefore, the first N-N in 3年中的所有牛到第 will add a heifer. dp [N-3].

N years later the total number of cattle is: have big cow dp [N-1] + new heifers dp [N-3].

 

 6. Matrix cover problem

We can use a small rectangle 2 * 1 sideways or vertically to cover a larger rectangle. Will the small rectangle of n 2 * 1 coverage without overlap a large rectangle 2 * n, a total of how many ways?

Skip steps similar to conventional thinking.

 

Guess you like

Origin www.cnblogs.com/dxtlearningblockchain/p/11519865.html