[Learning Dynamic Programming] The Nth Taibonacci Number (1)

Table of contents

How to learn dynamic programming?

1. Topic analysis

2. Algorithm principle

1. Status representation

2. State transition equation

3. Initialization

4. Filling order

5. Return value

3. Code writing

4. Space optimization

write at the end


How to learn dynamic programming?

There is no shortcut to learning an algorithm, let alone learning dynamic programming,

Brush dynamic programming algorithm questions with me, and learn dynamic programming together!

1. Topic analysis

Topic Link: 1137. The Nth Tibonacci Number - Leetcode

According to the conditions given by the topic:

Tn+3 = Tn + Tn+1 + Tn + 2, that is: Tn = Tn - 1 + Tn - 2 + Tn - 3

It can be known that the nth Taibonacci number is actually the sum of his first three numbers. 

2. Algorithm principle

1. Status representation

Generally speaking, we will first create an array as a dp table,

Fill up this dp table, and the answer is in a certain position on this table,

The meaning of the state representation is the meaning represented by a value on the table.

Not to mention these imaginary things, how do we get the state representation?

1. According to the requirements of the topic

2. Based on our experience + topic requirements

3. In the process of analyzing the problem, duplicate sub-problems are found

However, this topic is relatively simple, and we can directly obtain the state representation according to the requirements of the topic:

dp[ i ] means: the i-th Taibonacci number.

2. State transition equation

What is the state transition equation?

In fact it is:

dp[ i ] is equal to something.

This question is relatively simple. The question directly gives us the formula of the state transition equation.

So dp[ i ] is equal to:

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

3. Initialization

The functions of initialization are:

Make sure you don't cross the boundaries when filling out the form.

And this question is also very intimate for us:

He tells us: T0 = 0, T1 = 1, T2 = 1,

Then we only need to initialize: dp[ 0 ] = 0, dp[ 1 ] = 1, dp[ 2 ] = 1, that's it. 

4. Filling order

The order of filling in the form is for: when filling in the current state, the required state has already been calculated,

 Therefore, the order in which we fill in this question is from left to right.

5. Return value

In fact, the return value is to return the value required by the question. What this question returns is: dp[ n ]

3. Code writing

First look at the topic interface:

class Solution {
public:
    int tribonacci(int n) {
        
    }
};

Let's write the code in the order in which we just learned the algorithm principle:

class Solution {
public:
    int tribonacci(int n) {
        // 1. 创建 dp 表
        // 2. 初始化
        // 3. 填表
        // 4. 返回值

        // 处理边界问题
        if(n == 0) return 0;
        if(n == 1 || n == 2) return 1;

        vector<int> dp(n + 1);
        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];
    }
};

According to our four steps, we dealt with the boundary problem and passed it:

4. Space optimization

When we first started learning dynamic programming, the most important thing was how to solve this problem,

Instead of thinking about how to optimize, so I won’t focus on this later,

But now, taking advantage of the simplicity of this question, let's optimize it and bully this question.

Generally, the space optimization of dynamic programming is optimized by rolling arrays.

When we are filling in a dp table, we only need to use the first few states, and when other states are no longer needed,

We can then optimize using rolling arrays:

class Solution {
public:
    int tribonacci(int n) {
        //空间优化

        // 处理边界问题
        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;
    }
};

 In this way, the space consumption changes from O(N) to O(1).

Write at the end:

The above is the content of this article, thank you for reading.

If you feel that you have gained something, you can give the blogger a like .

If there are omissions or mistakes in the content of the article, please private message the blogger or point it out in the comment area~

Guess you like

Origin blog.csdn.net/Locky136/article/details/131513453