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. 00 python基础知识

    ''' ''' print('hello world!') ''' 变量 ''' # 变量的:‘tank’,会在内存中产生一份内存地址 #变量名:相当于一个门牌号,用于与变量进行绑定 # = :用来把 ...

  2. 35.分组聚合操作—bucket+metric

    主要知识点: bucket+metric 计算分种颜色的电视的平均价格     语法: GET /tvs/sales/_search { "size" : 0, "agg ...

  3. 【Codeforces 118B】Caesar's Legions

    [链接] 我是链接,点我呀:) [题意] 序列中不能连续出现k1个以上的1以及不能连续出现k2个以上的2,然后一共有n1个1以及n2和2,要求这n1+n2个数字都出现. 问序列有多少种可能. [题解] ...

  4. [bzoj3529][Sdoi2014]数表_树状数组_莫比乌斯反演

    数表 bzoj-3529 Sdoi-2014 题目大意:n*m的数表,第i行第j列的数是同时整除i和j的所有自然数之和.给定a,求数表中所有不超过a的和. 注释:$1\le n,m \le 10^5$ ...

  5. CF #328div2 D

    这题不难,当时想出来了,可是却写不出来~~ 现在慢慢写回来,也写得好挫~ 可以知道,被攻击的城市必定可以组成一棵树,然后,传送到的点必定也是城市之一.如果出发后回到原点,则需要2E,E是树的边数,则2 ...

  6. COCOS2D 学习笔记

    cc.Node.scheduleUpdate:function () * schedules the "update" method.                       ...

  7. VassistX 凝视 模板

    避免头文件反复包括宏定义: #ifndef $FILE_BASE_UPPER$_H_ #define $FILE_BASE_UPPER$_H_ $selected$ #endif // $FILE_B ...

  8. 新随笔(三)什么时候使用button,什么时候使用文字链接

    新随笔(三)什么时候使用button.什么时候使用文字链接 你为什么在这个地方用button而不用文字链接呢? 这是刚才我问一个设计师的问题. 她抬头看我,眼神迷茫.说:"没什么为什么呀,我 ...

  9. MySQL 日期时间函数大全 (转)

    转载自:http://blog.itpub.net/29773961/viewspace-1808967 以下内容基于MySQL 5.6及更高,大部分函数5.5也基本适用,更低版本请参考对应版本手册, ...

  10. jquery-layer弹出框

    样式1: 代码: 前台jsp: $("#add_table").bind("click",function(){ layer.open({ type: 2, t ...