[Learning Dynamic Programming] Three-step problem (2)

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

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

According to the topic, we can simulate the process of walking the stairs,

For example, here are four steps:

There is a way for children to walk up to a step, which is to go up directly:

There are two ways for children to walk up to the second step, one is to go up directly,

One is to use a step as a starting point and go up one step at a time:

 There are four ways for a child to walk up to the third step:

Walking directly up from flat ground is a way of walking;

Starting from a step, there is a way to get to a step, so walking directly up from a step is a way of walking;

Take the second step as the starting point, and there are two ways to get to the second step, so there are two methods starting from the second step;

1 + 1 + 2 = 4 ways:

 There are seven ways for a child to walk up to the four steps:

Starting from one step is a method;

Starting from the second step, there are two methods;

Starting with three steps, there are four methods:

1 + 2 + 4 = 7 ways:

Have you discovered the pattern now?

In fact, it is the addition of the first three numbers. 

2. Algorithm principle

1. Status representation

According to what we learned before, state representation refers to: what does dp[ i ] represent?

And dp[ i ] means: when reaching position i, how many ways there are in total.

2. State transition equation

So what is dp[ i ] equal to?

According to the law found above, it is not difficult to get:

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

3. Initialization

Initialization depends on what we just calculated:

dp[ 1 ] = 1,dp[ 2 ] = 2,dp[ 3 ] = 4。

4. Filling order

Go up the steps step by step, that is, fill in from left to right.

5. Return value

What is returned is the value of the dp[ n ] position

3. Code writing

Code writing is actually according to our previous analysis,

Just write it in a fixed routine:

class Solution {
public:
    int waysToStep(int n) {
        // dp 题目的固定写代码套路
        // 1. 创建dp表
        // 2. 初始化
        // 3. 填表
        // 4. 返回值

        const int MOD = 1e9 + 7;
        //考虑边界问题
        if(n == 1 || n == 2) return n;
        if(n == 3) return 4;

        vector<int> dp(n + 1);
        dp[1] = 1, dp[2] = 2, dp[3] = 4;
        for(int i = 4; i <= n; i++) {
            dp[i] = ((dp[i - 3] + dp[i - 2]) % MOD + dp[i - 1]) % MOD;
        }
        return dp[n];
    }
};

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/131513460