用dp解

1)up定义为nums[i-1] < nums[i]

  down nums[i-1] > nums[i]

  两个dp数组,

  up[i],记录包含nums[i]且nums[i-1] < nums[i]的最长子序列长度

  down[], 记录包含nums[i]nums[i-1] > nums[i]的最长子序列长度

2)更新方程

  有三种情况 nums[i-1] <or=or> nums[i]

  a) up[i] = down[i-1] + 1;

   down[i] = down[i-1]

  b) up[i] = up[i-1]

   down[i] = down[i-1]

  c) up[i] = up[i-1]

   down[i] = up[i-1] + 1;

  1. class Solution {
  2. public int wiggleMaxLength(int[] nums) {
  3. if(nums == null || nums.length == 0)
  4. return 0;
  5. int len = nums.length;
  6. int[] up = new int[len];
  7. int[] down = new int[len];
  8. int res = 1;
  9.  
  10. up[0] = 1;
  11. down[0] = 1;
  12.  
  13. for(int i=1; i<len; i++){
  14. if(nums[i] > nums[i-1]){
  15. up[i] = down[i-1] + 1;
  16. down[i] = down[i-1];
  17. }else if(nums[i] < nums[i-1]){
  18. up[i] = up[i-1];
  19. down[i] = up[i-1] + 1;
  20. }else{
  21. up[i] = up[i-1];
  22. down[i] = down[i-1];
  23. }
  24.  
  25. res = Math.max(res, Math.max(up[i], down[i]));
  26. }
  27.  
  28. return res;
  29.  
  30. }
  31. }

leetcode 376Wiggle Subsequence的更多相关文章

  1. [LeetCode] Is Subsequence 是子序列

    Given a string s and a string t, check if s is subsequence of t. You may assume that there is only l ...

  2. [LeetCode] Wiggle Subsequence 摆动子序列

    A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...

  3. LeetCode "Wiggle Subsequence" !

    Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, ...

  4. [LeetCode] Is Subsequence 题解

    前言 这道题的实现方法有很多,包括dp,贪心算法,二分搜索,普通实现等等. 题目 Given a string s and a string t, check if s is subsequence ...

  5. LeetCode——Is Subsequence

    Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...

  6. LeetCode "Is Subsequence"

    There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) co ...

  7. [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列

    A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...

  8. [LeetCode] Increasing Triplet Subsequence 递增的三元子序列

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

  9. [LeetCode] Longest Increasing Subsequence 最长递增子序列

    Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...

随机推荐

  1. 如何使用webpack 打包图片

    最近在学习vue,需要用到webpack打包css,在webpack中文网https://www.webpackjs.com/里只有css的打包配置, 在编写css样式时,因为要引入 背景图片,打包时 ...

  2. python 毫秒时间戳转日期

    import time import datetime timestamp = 1570774556514 # 转换成localtime time_local = time.localtime(tim ...

  3. 几个dp的陈年老题

    真 陈年老题 都是基础的dp优化 主要是展现我基础薄弱,菜得抠脚 1.四边形不等式 四边形不等式:w[i][j]+w[i+1][j+1]<=w[i+1][j]+w[i][j+1] 对于f[i][ ...

  4. 使用Windbg调试系统弹出的内存不可读错误

    步骤: 1. 使用Windbg挂钩到崩溃的进程上面 2. 使用~*k列出所有线程 3. 搜索UnhandledExceptionFilter所在的线程 4. 使用~ns切换到上面崩溃所在的线程,n为线 ...

  5. iOS开发UITableView随笔

    1.设置cell的间隔 - (void)setFrame:(CGRect)frame{ frame.size.height -=; [super setFrame:frame]; } 2.刷新row或 ...

  6. 微信小程序(mpvue框架) 购物车

    效果图: 说明:全选/全不选, 1.数据: products:[{checked:true,code:"4",echecked:false,hasPromotions:true,i ...

  7. SpringBoot项目中处理返回json的null值

    在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SS ...

  8. vue使用axios实现前后端通信

    安装依赖 npm install --save axios # vue-axios是对axios的简单封装 npm install --save vue-axios 用例 在main.js里面进行全局 ...

  9. POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)

    多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...

  10. springcloud Eureka Finchley.RELEASE 版本

    创建一个父项目cloud-demo pom.xml <?xml version="1.0" encoding="UTF-8"?> <proje ...