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, ...
随机推荐
- Hadoop配置lzo
编译: 0. 环境准备 maven(下载安装,配置环境变量,修改sitting.xml加阿里云镜像) gcc-c++ zlib-devel autoconf automake libtool 通过yu ...
- thinkphp5+GatewayWorker+Workerman
项目地址 ttps://www.workerman.net/workerman-chat thinkphp5+GatewayWorker+Workerman聊天室,可以多人聊天,指定某个人进行聊天, ...
- 【JZOJ4811】排队
description analysis 堆\(+\)树上倍增 考虑后序遍历搞出\(dfs\)序,那么要填肯定是从\(dfs\)序开始填 把每个点是序里第几位看成优先级,用小根堆来维护当前空着的优先级 ...
- 揭秘 Flink 1.9 新架构,Blink Planner 你会用了吗?
本文为 Apache Flink 新版本重大功能特性解读之 Flink SQL 系列文章的开篇,Flink SQL 系列文章由其核心贡献者们分享,涵盖基础知识.实践.调优.内部实现等各个方面,带你由浅 ...
- docker中国区镜像加速
[root@syzyy ~]# vim /etc/docker/daemon.json { "registry-mirros":[ "https://registry.d ...
- 微信小程序——单选项
对于小程序单选,官方文档已经贴出了代码,我这里也不做过多解释,只是分享给大家一个小功能 一般在单选或者多选时,都会出现“其他”这个选项,如果通过input焦点事件.失焦事件来控制,代码会很繁琐 这里可 ...
- 多进程并发socket通信
实现多个客户端同时接入server端,并且可以同时向客户端发送信息 server端 def dunc(conn,client_addr): while True: data=conn.recv(102 ...
- 创建 linuxrc 文件
创建 linuxrc,加入如下内容: [arm@localhost my_rootfs]#vi linuxrc #!/bin/sh #挂载/etc 为 ramfs, 并从/mnt/etc 下拷贝文件到 ...
- LINUX挂接Windows文件共享
Windows网络共享的核心是SMB/CIFS,在linux下要挂接(mount)windows的磁盘共享,就必须安装和使用samba 软件包.现在流行的linux发行版绝大多数已经包含了samba软 ...
- crontab中反引号和$()无效的解决
问题描述 1.增加了一条crontab,删除本月中2天以前的日志 10 02 * * * /bin/find /data/logs/php/$(date +%Y%m)/ -mtime +2 | x ...