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. 国密SSL证书免费试用申请指南

    沃通提供国密SSL证书免费申请试用服务,一次申请可同时签发SM2/RSA双算法证书,试用周期1个月,用于测试国密SM2 SSL证书的运行效果和SM2/RSA双证书部署效果. 试用产品:SM2/RSA双 ...

  2. ESX/ESXi 主机的某些存储阵列可能存在读取或写入性能问题 (1002598)

    Last Updated: 12/14/2018Categories: Troubleshooting  Details 免责声明:本文为 ESX/ESXi hosts might experienc ...

  3. sublime右键菜单,anaconda设置

    1.sublime_addright.inf [Version]Signature="$Windows NT$" [DefaultInstall]AddReg=SublimeTex ...

  4. [转]linux内存管理源码分析 - 页框分配器

    转自: http://www.cnblogs.com/tolimit/ 阅读之前,先敬原作者一杯! 分段和分页 先看一幅图 也就是我们实际中编码时遇到的内存地址并不是对应于实际内存上的地址,我们编码中 ...

  5. UVA1001 Say Cheese(Dijkstra或Floyd)

    题目链接:UVA1001 题意:在一个巨大奶酪中的A要以最短的时间与B相遇.在奶酪中走一米的距离花费的时间是10s,而奶酪中有许多洞,穿过这些洞的时间是0s.给出A.B以及各个洞的坐标,求最短的时间. ...

  6. java堆排序实现

    代码如下: public class HeapSort { public static void heapSort(DataWrap[] data) { System.out.println(&quo ...

  7. BZOJ 1602 牧场行走

    直接写一波Lca就好了 #include<cstdio> #include<cmath> #include<algorithm> using namespace s ...

  8. hdu2009 求数列的和【C++】

    求数列的和 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submi ...

  9. 多层gmetad配置

    经实验表明: ①多层gmetad与ganglia版本无关,且可以多版本兼容 ②多层gmetad只有最底层gmetad能保存详细指标,非底层gmetad收集到的都只能是summary信息,当然也许我配置 ...

  10. redis高可用,保证高并发

    目录 redis如何通过读写分离来承载读请求QPS超过10万+ redis replication以及master持久化对主从架构的安全意义 redis主从复制原理.断点续传.无磁盘化复制.过期key ...