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][0] = max(dp[k][1] + 1, dp[i][0]);
dp[i][1] = max(dp[k][0] + 1, dp[i][1]);
dp[i][0] represent the longest wanted subsequence with a positive sum ending;
dp[i][1] 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 == 0) return 0;int dp[n][2];for(int i = 0; i < n; i ++){dp[i][0] = dp[i][1] = 0;}int ans = 0;for(int i = 1; i < n; i ++){for(int k = 0; k < i; k ++){if(nums[i] > nums[k]){dp[i][0] = max(dp[i][0], dp[k][1] + 1);}else if(nums[i] < nums[k]){dp[i][1] = max(dp[i][1], dp[k][0] + 1);}}ans = max(dp[i][0], dp[i][1]);}return ans + 1;}
};

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

class Solution {
public:int wiggleMaxLength(vector<int>& nums) {const int n = nums.size();if(n == 0) return 0;int dp[n][2];//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[0][0] = dp[0][1] = 1;for(int i = 1; i < n; i ++){if(nums[i] > nums[i - 1]){dp[i][0] = dp[i - 1][1] + 1;dp[i][1] = dp[i - 1][1];}else if(nums[i] < nums[i -1]){dp[i][1] = dp[i - 1][0] + 1;dp[i][0] = dp[i - 1][0];}else{dp[i][0] = dp[i - 1][0];dp[i][1] = dp[i - 1][1];}} return max(dp[n - 1][0], dp[n - 1][1]);}
};

转载于:https://www.cnblogs.com/luntai/p/5899048.html

【Leetcode】376. Wiggle Subsequence相关推荐

  1. 【LeetCode】376. 摆动序列(图解)

    376. 摆动序列 一.问题 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4, ...

  2. 【LeetCode】动态规划入门(专项打卡21天合集)

    [LeetCode]动态规划入门(专项打卡21天合集) 下图为证 文章目录 [LeetCode]动态规划入门(专项打卡21天合集) Day1 斐波拉契数 第 N 个泰波那契数 Day2 爬楼梯 使用最 ...

  3. 【Leetcode】100. 相同的树

    题目 给定两个二叉树,编写一个函数来检验它们是否相同. 如果两个树在结构上相同,并且节点具有相同的值,则认为它们是相同的. 示例 1: 输入: 1 1/ \ / \2 3 2 3[1,2,3], [1 ...

  4. 【leetcode】85. Maximal Rectangle 0/1矩阵的最大全1子矩阵

    1. 题目 Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle containing only 1 ...

  5. 【leetcode】486. Predict the Winner

    题目如下: Given an array of scores that are non-negative integers. Player 1 picks one of the numbers fro ...

  6. 【leetcode】132. Palindrome Partitioning II

    题目如下: 解题思路:本题是[leetcode]131. Palindrome Partitioning的升级版,要求的是求出最小cuts,如果用[leetcode]131. Palindrome P ...

  7. 【leetcode】86. Partition List

    题目如下: Given a linked list and a value x, partition it such that all nodes less than x come before no ...

  8. 【Leetcode】103. 二叉树的锯齿形层次遍历

    题目 给定一个二叉树,返回其节点值的锯齿形层次遍历.(即先从左往右,再从右往左进行下一层遍历,以此类推,层与层之间交替进行). 例如: 给定二叉树 [3,9,20,null,null,15,7], 3 ...

  9. 【Leetcode】79.单词搜索

    题目 给定一个二维网格和一个单词,找出该单词是否存在于网格中. 单词必须按照字母顺序,通过相邻的单元格内的字母构成,其中"相邻"单元格是那些水平相邻或垂直相邻的单元格.同一个单元格 ...

最新文章

  1. 安装模拟器遇到的问题
  2. 使用fontTools库
  3. python编程计算1!+2!+...+10!_如何用C语言编程计算 1!+2!+3!+…+10!?
  4. javaScript DOM编程常用的方法与属性
  5. C++中const关键字的使用总结
  6. 分享Db4o的便捷封装类源码
  7. fir.im Weekly - 技术人也要苦练“七十二变”
  8. java快捷键大全,非常详细,清楚明了
  9. B站【云E办】在线办公系统 项目源码
  10. STL inserter
  11. R语言使用pROC包的的plot.roc函数对单变量进行ROC分析并可视化ROC曲线、寻找最佳阈值(threshold、cutoff)、在可视化曲线中添加最佳阈值点
  12. [回溯法] 和尚挑水问题-华为笔试
  13. 单片机笔记十一:华大单片机
  14. 利用python声音处理库librosa提取声音信号的mfcc特征及特征融合
  15. 进程篇——了解Makefile文件
  16. 对国家最新战略“新基建”的解读,一起把握赚钱的机会
  17. [jQuery]黑马课程学习笔记(一篇完)
  18. 人工智能正在向经济学领域渗透
  19. 欧拉函数积性性的证明
  20. 为什么说制造业需要MES系统?

热门文章

  1. python 办公自动化-python办公自动化:Excel操作入门
  2. python与c语言在语法上的区别-C语言和Python编程先学习哪个
  3. python turtle循环图案-Python绘图Turtle库详解
  4. php和python区别-编程语言之PHP与Python之间的差异
  5. python生成折线图-Python数据可视化 -生成数据之绘制折线图和散点图
  6. python下载word文件-Python用python-docx读写word文档
  7. python3项目-终于找到python3项目实战教程
  8. python学到什么程度可以做兼职-Python学到什么程度就可以找工作?
  9. python画柱状图和折线图-Python读取Excel表格,并同时画折线图和柱状图的方法
  10. 济南python工资一般多少钱-济南python编程课程培训哪家好