376 Wiggle Subsequence

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);
}

 

上一篇:List在执行add和remove方法是抛出java.lang.UnsupportedOperationException异常(已解决)


下一篇:java waffle java.lang.UnsatisfiedLinkError