leetcode 376Wiggle Subsequence
用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的更多相关文章
- [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 ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- LeetCode "Wiggle Subsequence" !
Another interesting DP. Lesson learnt: how you define state is crucial.. 1. if DP[i] is defined as, ...
- [LeetCode] Is Subsequence 题解
前言 这道题的实现方法有很多,包括dp,贪心算法,二分搜索,普通实现等等. 题目 Given a string s and a string t, check if s is subsequence ...
- LeetCode——Is Subsequence
Question Given a string s and a string t, check if s is subsequence of t. You may assume that there ...
- LeetCode "Is Subsequence"
There are 3 possible approaches: DP, divide&conquer and greedy. And apparently, DP has O(n^2) co ...
- [LeetCode] Arithmetic Slices II - Subsequence 算数切片之二 - 子序列
A sequence of numbers is called arithmetic if it consists of at least three elements and if the diff ...
- [LeetCode] Increasing Triplet Subsequence 递增的三元子序列
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- [LeetCode] Longest Increasing Subsequence 最长递增子序列
Given an unsorted array of integers, find the length of longest increasing subsequence. For example, ...
随机推荐
- double to string 损失精度的问题
https://blog.csdn.net/magieclarence/article/details/6792511?utm_source=blogxgwz0 类似于这样 double 的精度是一个 ...
- Go语言简介以及安装
http://www.runoob.com/go/go-tutorial.html Go 是一个开源的编程语言,它能让构造简单.可靠且高效的软件变得容易. Go是从2007年末由Robert Grie ...
- Perl 环境安装
Perl 环境安装 在我们开始学习 Perl 语言前,我们需要先安装 Perl 的执行环境. Perl 可以在以下平台下运行: Unix (Solaris, Linux, FreeBSD, AIX, ...
- 什么是存根类 Stub
转:http://www.cnblogs.com/cy163/archive/2009/08/04/1539077.html 存根类是一个类,它实现了一个接口,但是实现后的每个方法都是空的. ...
- (15)python打包
.py文件在没有安装python软件的电脑上是不能被执行的
- re.groups取出来的空元祖??
源自学习笔记: day23_1_re_ groups方法取出来的字符是空的元组??为啥? ''' # ------------------------------------------------- ...
- [AHOI2014/JSOI2014]骑士游戏
题目 思博贪心题写了一个半小时没救了,我也没看出这是一个\(spfa\)来啊 设\(dp_i\)表示彻底干掉第\(i\)只怪物的最小花费,一个非常显然的事情,就是对于\(k_i\)值最小的怪物满足\( ...
- 18-6-calsslist
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- SpringCloud学习笔记《---03 Ribbon---》基础篇
- idea设置编码格式utf-8
settings >> File Encodings >>如下