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, ...
随机推荐
- ps -aux|grep mysql时候报错:Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ
ps -aux|grep mysql时候报错:Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.8/FAQ ...
- 校园商铺-2项目设计和框架搭建-8升级mysql驱动相关的配置以支持mysql8
1.如何升级驱动 1.1步骤: a 确保当前程序能正常访问数据库 b 更新mysql驱动重新运行程序进行校验 maven依赖https://mvnrepository.com/artifact/mys ...
- dos中文显示乱码怎么办?
其实只需要一条命令 chcp 65001 执行该操作后,代码页就被变成UTF-8了 也可是GBK, 命令式: chcp 936 2.修改窗口属性,改变字体 在命令行标题栏上点击右键,选择&quo ...
- HDU-1492-The number of divisors(约数) about Humble Numbers -求因子总数+唯一分解定理的变形
A number whose only prime factors are 2,3,5 or 7 is called a humble number. The sequence 1, 2, 3, 4, ...
- HDU-1501-Zipper-字符串的dfs
Given three strings, you are to determine whether the third string can be formed by combining the ch ...
- JVM规范
- 初识OpenCV-Python - 009: 图像梯度
本节学习找到图像的梯度和边界.只要用到的函数为: cv2.Sobel(), cv2.Scharr(), cv2.Laplacian() 1. Laplacian 和 Sobel的对比 import c ...
- linux nload命令简介及安装方法
第一步:nload命令简介 nload 命令用于查看linux网络流量状况,实时输出.可以理解为是一个控制台应用程序,用来实时监测网络流量和带宽使用情况的命令. 使用可视化的界面显示流入和留出系统的网 ...
- 使用 /proc 文件系统
/proc 文件系统是一个特殊的软件创建的文件系统, 内核用来输出消息到外界. /proc 下 的每个文件都绑到一个内核函数上, 当文件被读的时候即时产生文件内容. 我们已经见到 一些这样的文件起作用 ...
- Matlab AlexNet 识别花
1. 首先,你要又并行计算的工具箱,在插件选项里面找到,安装即可 2. 下载训练的数据集,采用matlab演示的材料即可 https://matlabacademy-content.mathworks ...