376 Wiggle Subsequence

博客围绕求整数序列中最长摆动子序列长度展开。摆动序列要求相邻数差值正负交替。通过分析可知这是典型的动态规划问题,需考虑小项开头和大项开头两种数列情况,对任意元素向左寻找能连接到两种数列的最近元素,最后给出动态规划结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1 题目

A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.

For example, [1,7,4,9,2,5] is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast, [1,4,7,2,5] and [1,7,4,5,5] are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.

Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.

Example 1:

Input: [1,7,4,9,2,5]
Output: 6
Explanation: The entire sequence is a wiggle sequence.

Example 2:

Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
Explanation: There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8].

Example 3:

Input: [1,2,3,4,5,6,7,8,9]
Output: 2

2 尝试解

2.1 分析

给定一个整数向量,问不改变元素相对顺序的情况下,能构成的最大振荡数列的长度。

振荡数列随向量的扩张是连续变化的,所以这是一个典型的动态规划问题,最长子列可能是小→大→小→大,那么奇数项是小项,偶数项是大项;也可能是大→小→大→小,那么奇数项是大项,偶数项是小项。所以,对于任意元素nums[i],向左寻找能连接到两种数列的最近一个元素。如对于元素15,如果放在小项开头的数列中,

  • 如果作为偶数项加入,则要找到前一个比他小的元素,且该元素在数列中为奇数项,即元素5
  • 如果作为奇数项加入,则要找到前一个比他大的元素,且该元素在数列中为偶数项,即元素17

只要找到就停止寻找,所以找到5就会停止,即15作为第4项加在小项开头数列中。大项开头的数列同理。

动态规划完成,结果如下所示:

                   1 17 5 10 13 15 10 5 16 8

小项开头    1  2  3  4   4   4   5  5  6  7

大项开头    1  1  2  3  3   3   4   4  5  6

2.2 代码

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        vector<int> first_big(nums.size(),1);
        vector<int> first_small(nums.size(),1);
        int result = nums.size()? 1 : 0;
        for(int i = 1; i < nums.size(); i++){
            for(int j = i - 1; j >= 0; j--){
                if((nums[j] > nums[i] && first_small[j] % 2 == 0)||(nums[j] < nums[i] && first_small[j] % 2 == 1)){
                    first_small[i] = first_small[j] + 1;
                    if(first_small[i] > result)
                        result = first_small[i];
                    break;
                }
            }
            for(int j = i - 1; j >= 0; j--){
                if((nums[j] > nums[i] && first_big[j]%2 == 1)|| (nums[j] < nums[i] && first_big[j] % 2 == 0)){
                    first_big[i] = first_big[j] + 1;
                    if(first_big[i] > result)
                        result = first_big[i];
                    break;
                }
            }
        }
        return result;
    }
};

3 标准解

int wiggleMaxLength(vector<int>& nums) {
    int size = nums.size();
    if (size == 0) return 0;
    int up = 1, down = 1;
    
    for (int i = 1; i < size; ++i) {
        if (nums[i] > nums[i-1]) {
            up = down + 1;
        } else if (nums[i] < nums[i-1]) {
            down = up + 1;
        }
    }
    return max(up, down);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值