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. 27.7 并行语言集成查询(PLinq)

    static void Main() { ObsoleteMethods(Assembly.Load("mscorlib.dll")); Console.ReadKey(); } ...

  2. 【VIP视频网站项目】VIP视频网站项目v1.0.3版本发布啦(程序一键安装+电影后台自动抓取+代码结构调整)

    在线体验地址:http://vip.52tech.tech/ GIthub源码:https://github.com/xiugangzhang/vip.github.io 项目预览 主页面 登录页面 ...

  3. 数据结构与算法(6) -- heap

    binary heap就是一种complete binary tree(完全二叉树).也就是说,整棵binary tree除了最底层的叶节点之外,都是满的.而最底层的叶节点由左至右又不得有空隙. 以上 ...

  4. enote笔记语言(5)——其他

    章节:其他   ((主:单词))                               用来醒目地强调这个句子中哪个词语作主语 sentence:                         ...

  5. 学习MPI并行编程记录

    简单的MPI程序示例 首先,我们来看一个简单的MPI程序实例.如同我们学习各种语言的第一个程序一样,对于MPI的第一个程序同样是"Hello Word". /* Case 1 he ...

  6. Vmware在NAT模式下网络配置详解

    Vmware在NAT模式下网络配置详解 Linux中的网络配置对于接触Linux不久的小白菜来说,还是小有难度的,可能是不熟悉这种与windows系列迥然不同的命令行操作,也可能是由于对Linux的结 ...

  7. 亚马逊免费服务器搭建Discuz!论坛过程(四)

    上述命令还可能因缺少包引发其他错误: 如果出错则安装对应的包即可. 以下供参考: yum install libxml2 yum install libxml2-devel -y yum instal ...

  8. (问题待解决)sql查询不显示前置‘0’的问题

    SELECT * , ,),() ) ' end)) from CourseBooksNotes ),)),)+'秒'; ),)),)),)+'秒'

  9. Django——13 Auth系统 登陆注册实例 权限的实现

    Django Auth系统中的表 注册登陆实例 权限的实现 登陆权限 操作权限 组操作  Auth系统中的表 从表的名称我们就能看出,auth_user,auth_group,auth_permiss ...

  10. springCloud学习-服务链路追踪(Spring Cloud Sleuth)

    1.简介 Spring Cloud Sleuth 是 Spring Cloud 的一个组件,它的主要功能是在分布式系统中提供服务链路追踪的解决方案. 常见的链路追踪组件有 Google 的 Dappe ...