[Dynamic Programming Introductory Practice Manual] - Taibonacci Number (Rolling Array Space Optimization) | Three-step problem

1. Dynamic programming ideas to solve problems

  • 1. State representation
    The state representation of dynamic programming, in layman's terms, is to determine the meaning of the dp array .

  • 2. State transition equation
    The state transition equation is generally speaking: the recursive formula for determining the dp array

  • 3. Determine how to initialize

  • 4. Determine the traversal order

  • 5. Return value

2.Taiwanako number

topic

insert image description here

Solution ideas

  • 1. Determine the meaning of dp[i]
    For this question, the title clearly indicates that Tn is equivalent to dp[i] here, so the meaning of the dp array is clearly given.

dp[i] means: the value of the i-th Taibonacci number is dp[i]

  • 2. Determine the recursive formula:

The title has been given:
dp[i] = dp[i-1] + dp[i-2] + dp[i-3]

  • 3. Determine how to initialize

As can be seen from the title, the initialization sequence is: dp[0] = 0, dp[1] = 1, dp[2] = 1

  • 4. Determine the traversal order

Starting from i = 3, each Tibonacci number is obtained by adding its first three Tibonacci numbers,
so we should traverse from front to back.

  • 5. Return value

The return value is dp[n]


insert image description here

The specific code is as follows:

class Solution {
    
    
public:
    int tribonacci(int n) 
    {
    
    
        if(n < 3 && n!=0)
            return 1;
        else if(n == 0)
            return 0;
          
        dp[0] = 0,dp[1] = 1,dp[2] = 1;
        
        for(int i = 3;i<=n;i++)
        {
    
    
            dp[i] = dp[i-1] + dp[i-2] + dp[i-3]; 
        }

        return dp[n];
    }
};

时间复杂度O(n),空间复杂度O(n)

Space Optimization: Rolling Arrays

In this question, we know that every Taibonacci number is derived from the first three Taibonacci numbers, so we can derive the following numbers as long as we know the first three Taibonacci numbers, so we can First use three variables to save the first three Tibonacci numbers, then find the fourth Tibonacci number and save it in the variable, and then move these four number variables backward to form a rolling array.
insert image description here

The specific code is as follows:

class Solution {
    
    
public:
    int tribonacci(int n) 
    {
    
    
        //空间优化:滚动数组
        //    i:  0  1  2  3  4  5
        //dp[i]:   0  1  1  2  4  7
        //向后滚动
        if(n == 0)
            return 0;
        if(n == 1 || n == 2)
            return 1;
        int a =0,b=1,c=1,d=0;
        for(int i = 3;i<=n;i++)
        {
    
    
            d = a + b + c;
            a = b,b = c,c = d;
        }
        return d;
    }
};

时间复杂度O(n),空间复杂度O(1)

Three, three-step questions

link here

insert image description here

Solution ideas

  • 1. Determine the meaning of dp[i]

According to the meaning of the question, we can know that there is one way for a child to walk up the first step, and there are two ways for a child to walk up the second step,
so we can be sure

dp[i] means: there are dp[i] ways to walk the i-th step

  • 2. Determine the recursive formula:

According to the meaning of the title, dp[1] = 1, dp[2] = 2, dp[3] = 4; that is, there are
4 ways to go to the third step, namely:

  • 1. Take 1 step every time
    2. Take 1 step for the first time, 2 steps for the second time
    3. Take 2 steps for the first time, 1 step for the second time
    4. Take 3 steps at a time

So how many ways are there to walk the 4th step?
We know that the fourth method must be derived from the following three situations:

  • 1. Take 3 steps from the first floor to the 4th floor
  • 2. Take 2 steps from the second floor to the 4th floor
  • 3. Take one step from the third floor to the fourth floor

We already know that there is one way to walk the first step, two ways to walk the second step, and four ways to walk the third step.
Then, dp[4] = dp[1] + dp[2] + dp[3]

If you understand it like this:

dp[4] = dp[1] + 1 + dp[2] + 1 + dp[3] + 1

You have not figured out what the meaning of dp[i] is, dp[i] means that there are dp[i] ways to walk the i-th step

If you understand it this way, the meaning is how many steps it takes to get to the 4th step.

So the number of ways to get to the i-th step is the result of adding the number of ways to the i-1 + i-2 + i-3 steps.

The recursive formula is:

dp[i] = dp[i-1] + dp[i-2] + dp[i-3]

  • 3. Determine how to initialize

As can be seen from the title, the initialization sequence is: dp[1] = 1, dp[2] = 2, dp[3] = 4

  • 4. Determine the traversal order

Starting from i = 4, the number of ways to go to each step is the sum of the number of ways of the first 3 steps.
So we should traverse from front to back.

  • 5. Return value

The return value is dp[n]

Note: dp[0] means that the number of ways to go to the 0th step is dp[0], which has no meaning

class Solution 
{
    
    
public:
    int waysToStep(int n) 
    {
    
    
        if(n == 3)
            return 4;
        else if(n < 3)
            return n ;
        vector<long long> dp(n+1);
        dp[1] = 1,dp[2] = 2,dp[3] =4;
        for(int i = 4;i <= n;i++)
        {
    
    
            dp[i] = (dp[i-1] + dp[i-2] + dp[i-3]) % 1000000007;
        }

        return dp[n] % 1000000007;
    }
};

Summarize

Start learning the relevant content of dynamic programming today, and it will be updated continuously in the future. It is a long-term process, just stick to it!

Guess you like

Origin blog.csdn.net/2301_76496134/article/details/131464141