Description:  

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.

Examples:

Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence. Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8]. Input: [1,2,3,4,5,6,7,8,9]
Output: 2

  I got this problem by mocking which was given 40 mins. However failed,  WTF! At the begining, I concluded it was an dp problem. I was stuck in how to solve it in one loop n(O(n) timespace). then I try to figure out the trans-fomula:

dp[i][] = max(dp[k][] + , dp[i][]);
dp[i][] = max(dp[k][] + , dp[i][]);
dp[i][] represent the longest wanted subsequence with a positive sum ending;
dp[i][] similarly but with a negative sum ending;

  You must solve it in time which may sacrifice the timespace!

class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
const int n = nums.size();
if(n == ) return ;
int dp[n][];
for(int i = ; i < n; i ++){
dp[i][] = dp[i][] = ;
}int ans = ;
for(int i = ; i < n; i ++){
for(int k = ; k < i; k ++){
if(nums[i] > nums[k]){
dp[i][] = max(dp[i][], dp[k][] + );
}else if(nums[i] < nums[k]){
dp[i][] = max(dp[i][], dp[k][] + );
}
}
ans = max(dp[i][], dp[i][]);
}
return ans + ;
}
};

  Finally, I optimize the solution to O(n).

class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
const int n = nums.size();
if(n == ) return ;
int dp[n][];
//dp[i][0] : Before i the longest wanted subsequence ending with a positive ending.
//dp[i][1] : Before i the longest wanted subsequence ending with a negative ending.
dp[][] = dp[][] = ;
for(int i = ; i < n; i ++){
if(nums[i] > nums[i - ]){
dp[i][] = dp[i - ][] + ;
dp[i][] = dp[i - ][];
}else if(nums[i] < nums[i -]){
dp[i][] = dp[i - ][] + ;
dp[i][] = dp[i - ][];
}else{
dp[i][] = dp[i - ][];
dp[i][] = dp[i - ][];
}
}
return max(dp[n - ][], dp[n - ][]);
}
};

【Leetcode】376. Wiggle Subsequence的更多相关文章

  1. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  2. 【LeetCode】392. Is Subsequence 解题报告(Python)

    [LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...

  3. 【leetcode】1081. Smallest Subsequence of Distinct Characters

    题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...

  4. 【LeetCode】Increasing Triplet Subsequence(334)

    1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...

  5. 【leetcode】Increasing Triplet Subsequence

    Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...

  6. 【leetcode】280.Wiggle Sort

    原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...

  7. 【LeetCode】280. Wiggle Sort 解题报告 (C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...

  8. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  9. 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)

    [LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...

随机推荐

  1. Java类及成员

    Java类及成员 类 类是对一类事物的的描述,是抽象的概念上的定义:类是创建对象的模板: public class TestClass { public static void main(String ...

  2. P1352 没有上司的舞会——树形DP入门

    P1352 没有上司的舞会 题目描述 某大学有N个职员,编号为1~N.他们之间有从属关系,也就是说他们的关系就像一棵以校长为根的树,父结点就是子结点的直接上司.现在有个周年庆宴会,宴会每邀请来一个职员 ...

  3. 【codeforces 508B】Anton and currency you all know

    [题目链接]:http://codeforces.com/contest/508/problem/B [题意] 给你一个奇数; 让你交换一次数字; 使得这个数字变成偶数; 要求偶数要最大; [题解] ...

  4. [bzoj1923]外星千足虫[高斯消元]

    高斯消元解异或方程组 #include <iostream> #include <algorithm> #include <cstdio> #include < ...

  5. 神奇的幻方 noip2015day1 T1

     题目描述 Description 幻方是一种很神奇的N∗N矩阵:它由数字 1,2,3, … … ,N∗N构成,且每行.每列及两条对角线上的数字之和都相同. 当N为奇数时,我们可以通过以下方法构建一个 ...

  6. 输入法InputConnection

    /**  * The InputConnection interface is the communication channel from an  * {@link InputMethod} bac ...

  7. java 调用 库文件错误查找方法

    第一步首先找到 backtrace:keyword,然后找到都应的库文件 出错的地方 pc 0000088b  /system/lib/libNDK_04.so (SayHello+98). 08-1 ...

  8. Rails 教程

    Rails 教程 http://web.siwei.tech/ http://www.siwei.me/

  9. android注解使用具体解释(图文)

    在使用Java的SSH框架的时候,一直在感叹注解真是方便啊,关于注解的原理,大家能够參考我的还有一片文章Java注解具体解释. 近期有时间研究了android注解的使用,今天与大家分享一下. andr ...

  10. WPF学习笔记——DataContext 与 ItemSource

    作为一个WPF新手,在ListBox控件里,我分不清 DataContext 与 ItemSource的区别. 在实践中,似乎: <ListBox x:Name="Lst" ...