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, ...
随机推荐
- IDEA快捷键(收集自网络后整理)
快捷键 说明 CTRL+B 快速打开光标处的类或方法 CTRL+C 拷贝 CTRL+D 复制当前行到下一行 CTRL+E 最近打开的文件 CTRL+F 当前文件查找特定内容 CTRL+G 定位行 CT ...
- Wed Nov 01 13:03:16 CST 2017 WARN: Establishing SSL connection without server's identity verification is not recommended.
报错:Wed Nov 01 13:03:16 CST 2017 WARN: Establishing SSL connection without server's identity verifica ...
- (37)C#Linq
https://docs.microsoft.com/zh-cn/dotnet/csharp/language-reference/keywords/let-clause 一.定义 Linq(Lang ...
- org.apache.ibatis.binding.BindingException: Parameter 'xxx' not found. Available parameters are [arg1, arg0, param1, param2]
这个异常说明参数没有加上@Param注解,加上这个注解就行了. 默认情况下mybatis把参数按顺序转化为[0, 1, param1, param2],也就是说#{0} 和 #{param1} 是一样 ...
- day 51 阿里iconfont的使用
阿里iconfont的使用 1. 找到阿里巴巴图标库 2.找到图标 3.搜索你想要的图标 4.将图标添加到购物车 5.点击右上角的购物车按钮,我这里添加了两个. 6.提示你登陆,不需要花钱,找其中 ...
- Caused by: java.lang.NoSuchMethodError: org.hibernate.engine.spi.SessionFactoryImplementor.getProperties()Ljava/util/Properties;
报错信息 Error starting ApplicationContext. To display the auto-configuration report re-run your applica ...
- 4_3.springboot2.x之默认访问首页和国际化
1.默认访问首页 1.引入thymeleaf和引入bootstrap <!--引入thymeleaf--> <dependency> <groupId>org.sp ...
- 第二周课堂笔记2th
---恢复内容开始--- 1. 2.索引取单个值 取多个值叫切片, 切片:取多个值 从左到右取值: 原则:顾头不顾尾 1, a[0:3] abc 2, a[-5:-2] abc 3, a[0:-2] ...
- java ajax长连接请求服务器数据
Servlet 3.0笔记之异步请求Comet推送长轮询(long polling)篇 Comet另一种形式为长轮询(long polling),客户端会与服务器建立一个持久的连接,直到服务器端有数据 ...
- 44道JS难题
国外某网站给出了44道JS难题,试着做了下,只做对了17道.这些题涉及面非常广,涵盖JS原型.函数细节.强制转换.闭包等知识,而且都是非常细节的东西,透过这些小细节可以折射出很多高级的JS知识点. 你 ...