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, ...
随机推荐
- 如何使用webpack 打包图片
最近在学习vue,需要用到webpack打包css,在webpack中文网https://www.webpackjs.com/里只有css的打包配置, 在编写css样式时,因为要引入 背景图片,打包时 ...
- python 毫秒时间戳转日期
import time import datetime timestamp = 1570774556514 # 转换成localtime time_local = time.localtime(tim ...
- 几个dp的陈年老题
真 陈年老题 都是基础的dp优化 主要是展现我基础薄弱,菜得抠脚 1.四边形不等式 四边形不等式:w[i][j]+w[i+1][j+1]<=w[i+1][j]+w[i][j+1] 对于f[i][ ...
- 使用Windbg调试系统弹出的内存不可读错误
步骤: 1. 使用Windbg挂钩到崩溃的进程上面 2. 使用~*k列出所有线程 3. 搜索UnhandledExceptionFilter所在的线程 4. 使用~ns切换到上面崩溃所在的线程,n为线 ...
- iOS开发UITableView随笔
1.设置cell的间隔 - (void)setFrame:(CGRect)frame{ frame.size.height -=; [super setFrame:frame]; } 2.刷新row或 ...
- 微信小程序(mpvue框架) 购物车
效果图: 说明:全选/全不选, 1.数据: products:[{checked:true,code:"4",echecked:false,hasPromotions:true,i ...
- SpringBoot项目中处理返回json的null值
在后端数据接口项目开发中,经常遇到返回的数据中有null值,导致前端需要进行判断处理,否则容易出现undefined的情况,如何便捷的将null值转换为空字符串? 以SpringBoot项目为例,SS ...
- vue使用axios实现前后端通信
安装依赖 npm install --save axios # vue-axios是对axios的简单封装 npm install --save vue-axios 用例 在main.js里面进行全局 ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
- springcloud Eureka Finchley.RELEASE 版本
创建一个父项目cloud-demo pom.xml <?xml version="1.0" encoding="UTF-8"?> <proje ...