用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;

 class Solution {
public int wiggleMaxLength(int[] nums) {
if(nums == null || nums.length == 0)
return 0;
int len = nums.length;
int[] up = new int[len];
int[] down = new int[len];
int res = 1; up[0] = 1;
down[0] = 1; for(int i=1; i<len; i++){
if(nums[i] > nums[i-1]){
up[i] = down[i-1] + 1;
down[i] = down[i-1];
}else if(nums[i] < nums[i-1]){
up[i] = up[i-1];
down[i] = up[i-1] + 1;
}else{
up[i] = up[i-1];
down[i] = down[i-1];
} res = Math.max(res, Math.max(up[i], down[i]));
} return res; }
}

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. javaScript 习题总结(持续更新)

    判定偶数 function collect_all_even(collection) { return collection.filter(item => item%2 == 0); } 两个集 ...

  2. 暴力剪枝——cf1181C

    暴力求长度为len时,以i,j为左上角的旗子的数量 不剪枝的话复杂度是n*n*m*n,必定超时 两个可以剪枝的地方:如果格子[i,j]可以作为长度为len的旗子的左上角,那么其必定不可以作为长度> ...

  3. 模拟求root——cf1067B

    注意最后一轮要单独求一下 且最后只能有一个root #include <bits/stdc++.h> using namespace std; #define MOD 1000000007 ...

  4. tornado接收ajax的post请求报错WARNING:tornado.access:405 OPTIONS /add

    后端报错信息 WARNING:tornado.access:405 OPTIONS /add (::1) 1.00m 前端报错信息 2xhr.js?ec6c:172 OPTIONS http://lo ...

  5. SSM14-通过AOP实现日志记录

    1.要求使用AOP思想,实现对每一个用户登陆后,将以下信息保存在数据库 1>登陆时间 2>退出时间 3>登录的IP地址 4>访问点URL(访问了那些Controller) 5& ...

  6. csp-s模拟测试89

    csp-s模拟测试89 $T1$想了一会儿没什么思路,一看$T2$  $1e18$当场自闭打完暴力就弃了,$T3$看完题感觉要求$lca$和$dep$,手玩了一下样例发现$lca$很显然,$dep$貌 ...

  7. 多线程的基本概念和Delphi线程对象Tthread介绍

    多线程的基本概念和Delphi线程对象Tthread介绍 作者:xiaoru    WIN 98/NT/2000/XP是个多任务操作系统,也就是:一个进程可以划分为多个线程,每个线程轮流占用CPU运行 ...

  8. hexo 错误汇总

    文章目录 发布文章遇到: 发布文章的时候出现错误: 代码推送到github,hexo g -d 半天推送不上去 记录一次hexo+coding hexo s本都没问题,hexo g -d 样式并未改变 ...

  9. Python collection模块与深浅拷贝

    collection模块是对Python的通用内置容器:字典.列表.元组和集合的扩展,它包含一些专业的容器数据类型: Counter(计数器):dict子类,用于计算可哈希性对象的个数. Ordere ...

  10. 石子合并问题 /// 区间DP oj2025

    Description 在一个圆形操场的四周摆放着n堆石子.现要将石子有次序地合并成一堆. 规定每次只能选相邻的两堆石子合并成新的一堆,并将新得的这堆石子数记为该次合并的得分. 试设计一个算法,计算出 ...