【Leetcode】376. Wiggle Subsequence
Description:
A sequence of numbers is called a wiggle sequence if the differences between successive numbers strictly alternate between positive and negative. The first difference (if one exists) may be either positive or negative. A sequence with fewer than two elements is trivially a wiggle sequence.
For example, [1,7,4,9,2,5]
is a wiggle sequence because the differences (6,-3,5,-7,3) are alternately positive and negative. In contrast,[1,4,7,2,5]
and [1,7,4,5,5]
are not wiggle sequences, the first because its first two differences are positive and the second because its last difference is zero.
Given a sequence of integers, return the length of the longest subsequence that is a wiggle sequence. A subsequence is obtained by deleting some number of elements (eventually, also zero) from the original sequence, leaving the remaining elements in their original order.
Examples:
Input: [1,7,4,9,2,5]
Output: 6
The entire sequence is a wiggle sequence. Input: [1,17,5,10,13,15,10,5,16,8]
Output: 7
There are several subsequences that achieve this length. One is [1,17,10,13,10,16,8]. Input: [1,2,3,4,5,6,7,8,9]
Output: 2
I got this problem by mocking which was given 40 mins. However failed, WTF! At the begining, I concluded it was an dp problem. I was stuck in how to solve it in one loop n(O(n) timespace). then I try to figure out the trans-fomula:
dp[i][] = max(dp[k][] + , dp[i][]);
dp[i][] = max(dp[k][] + , dp[i][]);
dp[i][] represent the longest wanted subsequence with a positive sum ending;
dp[i][] similarly but with a negative sum ending;
You must solve it in time which may sacrifice the timespace!
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
const int n = nums.size();
if(n == ) return ;
int dp[n][];
for(int i = ; i < n; i ++){
dp[i][] = dp[i][] = ;
}int ans = ;
for(int i = ; i < n; i ++){
for(int k = ; k < i; k ++){
if(nums[i] > nums[k]){
dp[i][] = max(dp[i][], dp[k][] + );
}else if(nums[i] < nums[k]){
dp[i][] = max(dp[i][], dp[k][] + );
}
}
ans = max(dp[i][], dp[i][]);
}
return ans + ;
}
};
Finally, I optimize the solution to O(n).
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
const int n = nums.size();
if(n == ) return ;
int dp[n][];
//dp[i][0] : Before i the longest wanted subsequence ending with a positive ending.
//dp[i][1] : Before i the longest wanted subsequence ending with a negative ending.
dp[][] = dp[][] = ;
for(int i = ; i < n; i ++){
if(nums[i] > nums[i - ]){
dp[i][] = dp[i - ][] + ;
dp[i][] = dp[i - ][];
}else if(nums[i] < nums[i -]){
dp[i][] = dp[i - ][] + ;
dp[i][] = dp[i - ][];
}else{
dp[i][] = dp[i - ][];
dp[i][] = dp[i - ][];
}
}
return max(dp[n - ][], dp[n - ][]);
}
};
【Leetcode】376. Wiggle Subsequence的更多相关文章
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- 【LeetCode】392. Is Subsequence 解题报告(Python)
[LeetCode]392. Is Subsequence 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/is-subseq ...
- 【leetcode】1081. Smallest Subsequence of Distinct Characters
题目如下: Return the lexicographically smallest subsequence of text that contains all the distinct chara ...
- 【LeetCode】Increasing Triplet Subsequence(334)
1. Description Given an unsorted array return whether an increasing subsequence of length 3 exists o ...
- 【leetcode】Increasing Triplet Subsequence
Given an unsorted array return whether an increasing subsequence of length 3 exists or not in the ar ...
- 【leetcode】280.Wiggle Sort
原题 Given an unsorted array nums, reorder it in-place such that nums[0] <= nums[1] >= nums[2] & ...
- 【LeetCode】280. Wiggle Sort 解题报告 (C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 排序后交换相邻元素 日期 题目地址:https://l ...
- 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)
[LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...
- 【LeetCode】522. Longest Uncommon Subsequence II 解题报告(Python)
[LeetCode]522. Longest Uncommon Subsequence II 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxuemin ...
随机推荐
- nodejs 文件操作模块 fs
const fs=require("fs"); //文件操作 //创建目录 ./ 代表当前目录 ../ 代表上级目录fs.mkdir('./test',function(err){ ...
- 【转载】使用IntelliJ IDEA创建Maven聚合工程、创建resources文件夹、ssm框架整合、项目运行一体化
一.创建一个空的项目作为存放整个项目的路径 1.选择 File——>new——>Project ——>Empty Project 2.WorkspaceforTest为项目存放文件夹 ...
- jQuery练习:表单模态框
代码:基于事件冒泡原理和事件委托 <!DOCTYPE html> <html lang="zh-cn"> <head> <meta cha ...
- 一篇入门Node.js
目录 1.Node.js 简介 2.Node.js NPM 3.Node.js 模块 4.Node.js 事件 5.Node.js Buffer 6.Node.js 文件系统 7.Node.js St ...
- 编写who命令
第一个版本: /* who1.c - a first version of the who program * open, read UTMP file, and show results. */ # ...
- 用 console.time()和 console.timeEnd() 测试你的 javascript 代码执行效率
无意中学习到了一种测试 javascript 代码执行效率的一种方法,就记下来便于以后使用,用到了console对象中的 time 和 timeEnd 方法 . console.time('m ...
- 【郑轻邀请赛 C】DOBRI
[题目链接]:https://acm.zzuli.edu.cn/zzuliacm/problem.php?id=2129 [题意] [题解] NMB 直接说i-1,i-2,i-3不就好了- [Numb ...
- Oracle学习总结(5)—— SQL语句经典案例
--0.所有员工信息 SELECT * FROM emp --1.选择部门30的所有员工 SELECT * FROM emp WHERE deptno=20 --2.列出所有办事员(CLERK)的姓名 ...
- Java Web学习总结(23)——Distributed Configuration Management Platform(分布式配置管理平台)
专注于各种 分布式系统配置管理 的通用组件/通用平台, 提供统一的配置管理服务. 主要目标: 部署极其简单:同一个上线包,无须改动配置,即可在 多个环境中(RD/QA/PRODUCTION) 上线 部 ...
- [JZOJ4687]奇袭
[JZOJ4687]奇袭 题目 由于各种原因,桐人现在被困在Under World(以下简称UW)中,而UW马上要迎来最终的压力测试——魔界入侵. 唯一一个神一般存在的Administrator被消灭 ...