【leetcode】【medium】376. Wiggle Subsequence

230 篇文章 0 订阅

376. Wiggle Subsequence

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

Follow up:
Can you do it in O(n) time?

题目链接:https://leetcode-cn.com/problems/wiggle-subsequence/

 

思路

延续找最长上升序列的题的思路,这道题的情况稍微复杂一些,但可以通过尝试写样例找出规律:

1)需要记录当前最长序列的是处于上升还是下降阶段,以及最后一个数字的值;

2)每个数字对两种情况都进行判断,是否能够接在上升值后,或下降值后;

3)为了序列能最长,我们希望当处于上升阶段的最后一个数字越小越好(扩大下一次下降的取值范围),下降阶段的越大越好。

综上,我们需要一个空间, 分别记录上升结尾和下降结尾的子串,长度为i的子串们的最后一个数,其中的最大/最小值。

时间O(n^2),空间O(n)。

写这个代码的时候没有意识到,利用了3)就已经在实际运行中使得时间复杂度下降到O(n)了。

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int len = nums.size();
        if(len<=1) return len;
        int rec[len+1][2];
        for(int i=0; i<=len; ++i){
            rec[i][0] = INT_MAX;
            rec[i][1] = INT_MIN;
        }
        rec[1][0] = nums[0];
        rec[1][1] = nums[0];
        int res0=1, res1=1;
        for(int i=1; i<len; ++i){
            int j=res0;
            int pos0 = 0, pos1 = 0;
            for(; j>0; --j){
                if(rec[j][0]==nums[i]) break;
                if(rec[j][0]<nums[i]){
                    pos1 = j+1;
                    break;
                }
            }
            for(j=res1; j>0; --j){
                if(rec[j][1]==nums[i]) break;
                if(rec[j][1]>nums[i]){
                    pos0 = j+1;
                    break;
                }
            }
            if(pos1){
                rec[pos1][1] = max(rec[pos1][1], nums[i]);
                res1 = max(res1, pos1);
            }
            if(pos0){
                rec[pos0][0] = min(rec[pos0][0], nums[i]);
                res0 = max(res0, pos0);
            }
        }
        return max(res0, res1);
    }
};

 

优化

进一步地发现,其实利用了特点3)就已经能保证不需要向前遍历,只要记录最长子序列的状态和末尾数字即可,前面长度不需要记录。因为新数字只有2中情况:

1)能加入,则向后添加。

2)不能加入,则可以判断是否能更新当前最长子序列末尾数字,看能否扩大取之范围。

因此记录空间由数组降到单个变量,寻找位置的操作也省略了。

时间O(n),空间O(1)。

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int len = nums.size();
        if(len<=1) return len;
        int rec0 = nums[0], rec1 = nums[0];
        int res0=1, res1=1;
        for(int i=1; i<len; ++i){
            int pos1=0, pos0=0;
            if(rec0<nums[i]){
                pos1 = res0+1;
            }else{
                pos1 = res1;
            }
            if(rec1>nums[i]){
                pos0 = res1+1;
            }else{
                pos0 = res0;
            }
            rec1 = (pos1>res1)?nums[i]: max(rec1, nums[i]);
            res1 = max(res1, pos1);
            rec0 = (pos0>res0)?nums[i]: min(rec0, nums[i]);
            res0 = max(res0, pos0);
        }
        return max(res0, res1);
    }
};

 

优化

这个是看了分享才想到的。虽然上一方法已经时空都最小了,但在处理逻辑上有更加简洁的。

其实根本不用管最后一个值的大小,连续上升或连续下降的一段子串,可以看成它们只是一个数,直接忽略跳过即可!!

所以代码直接简化。

时间O(n),空间(1)。

class Solution {
public:
    int wiggleMaxLength(vector<int>& nums) {
        int len = nums.size();
        if(len<=1) return len;
        int res0 = 1, res1 = 1;
        for(int i=1; i<len; ++i){
            if(nums[i]>nums[i-1]){
                res1 = res0 + 1;
            }
            if(nums[i]<nums[i-1]){
                res0 = res1 + 1;
            }
        }
        return max(res0, res1);
    }
};

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值