用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. LR调试脚本的时候报错Error -27796:(已解决)

    LR调试bbs脚本的时候报错: 1.Error -27796: Failed to connect to server "192.168.211.128:80": [10060] ...

  2. [JZOJ3691] 【CF414E】Mashmokh's Designed tree

    题目 题目大意 给你一棵树,接下来对这棵树进行三种操作: 1.询问两点之间的距离. 2.让某个点变为它原来的第\(h\)个祖先的最后一个儿子. 3.求\(dfs\)序中最后一个深度为\(k\)的点. ...

  3. [JZOJ2679] 跨时代

    题目 题目大意 给你一堆边,你要将它们围成面积最大的矩形. 边不一定要用完,而且围成的矩形不能凸出一块. \(n\leq 16\) \(l_i \leq 15\) 思考历程 看到这题的第一眼,就会立马 ...

  4. spring整合Quartz框架过程,大家可以参考下

    这篇文章详细介绍了spring集成quartz框架流程,通过示例代码进行了详细说明,对学习或任务有参考学习价值,并可供需要的朋友参考. 1.quartz框架简介(m.0831jl.com) quart ...

  5. ros多机系统

    编辑从机的~/.bashrc添加如下指令 方法一 export ROS_MASTER_URI=http://qian:11311 #qian是主机的机器名 export ROS_HOSTNAME=ro ...

  6. 模式识别原理(Pattern Recognition)、概念、系统、特征选择和特征

    §1.1 模式识别的基本概念 一.广义定义 1.模式:一个客观事物的描述,一个可用来仿效的完善的例子. 2.模式识别:按哲学的定义是一个“外部信息到达感觉器官,并被转换成有意义的感觉经验”的过程. 例 ...

  7. 通过aapt查看apk包名和第一个启动的activity

    步骤: ps:aapt是sdk 自带的一个工具,在sdk\builds-tools目录下: 1. cmd启动控制台, 默认是c盘,输入“d:” 即可转到D盘目录 2. 到D盘后 输入cd 子文件目录转 ...

  8. 廖雪峰Java15JDBC编程-2SQL入门-1SQL介绍

    1.SQL:结构化查询语言 Structured Query Language 针对关系数据库设计 各种数据库基本一致 允许用户通过SQL查询数据而不关心数据库底层存储结构 1.1 SQL使用: 可以 ...

  9. DRF 请求生命周期以及各模块解析

    目录 rest_framework框架的封装特点 原生Django与DRF比较 APIView 的请求生命周期 请求模块(request) 解析模块(parser_classes) 异常模块(exce ...

  10. some方法过滤

    // 已经存在该tab时跳过 this.tabs.some(item => item.title === option.title) || this.tabs.push(option)