[Learn Dynamic Programming] Swing Sequence (27)

Table of contents

How to learn dynamic programming?

1. Question analysis

2. Algorithm principle

1. Status display

2. State transition equation

3. Initialization

4. Order of filling in the form

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.

Come with me to solve dynamic programming algorithm questions and learn dynamic programming together!

1. Question analysis

Question link: 376. Swing sequence - LeetCode 

This question is easy to understand. He needs to find that the difference between the numbers is an alternation of a positive number and a negative number.

In fact, we don't have to think about it so much. We can think of it as a sequence of increasing, decreasing, increasing, decreasing, and alternating.

Then don't forget that what you are looking for is a subsequence, and you can jump to find it.

2. Algorithm principle

1. Status display

dp[i] represents the length of the longest wobble sequence among all subsequences ending at position i.

But it is actually divided into two situations:

f [ i ] represents the length of the longest swing sequence ending at position i and showing an “upward” trend at the last position.

g[i] represents the length of the longest swing sequence ending at position i, with the last position showing a "downward" trend.

2. State transition equation

State transition equations are still divided into two major categories:

Let’s start with f[i]:

f[i] can itself be used as a subsequence, the length is 1

f [ i ] can become a subsequence together with any number in front of it, and the length is g [ i - 1 ] + 1

What should be noted here is that f[i-1]<f[i] is required

Then g[i]:

g[i] can itself be used as a subsequence, the length is 1

g [ i ] can become a subsequence together with any number in front of it, and the length is f [ i - 1 ] + 1

The thing to note here is that g[i-1]>g[i] is required

3. Initialization

As long as we set them all to 1, we don't need to consider the first situation.

4. Order of filling in the form

From left to right.

5. Return value

Returns the maximum value in the f table and g table.

3. Code writing

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int n = nums.size(), fmax = 1, gmax = 1;
        vector<int> f(n, 1), g(n, 1);
        for(int i = 1; i < n; i++) {
            for(int j = 0; j < i; j++) {
                if(nums[i] > nums[j]) f[i] = max(f[i], g[j] + 1);
                if(nums[i] < nums[j]) g[i] = max(g[i], f[j] + 1);
            }
            fmax = max(fmax, f[i]);
            gmax = max(gmax, g[i]);
        }
        return max(fmax, gmax);
    }
};

Write at the end:

That’s the content of this article, thank you for reading.

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

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

Guess you like

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