376 Wiggle Subsequence 摆动序列
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
Follow up:
Can you do it in O(n) time?
详见:https://leetcode.com/problems/wiggle-subsequence/description/
C++:
class Solution {
public:
int wiggleMaxLength(vector<int>& nums)
{
if (nums.empty())
{
return 0;
}
vector<int> p(nums.size(), 1);
vector<int> q(nums.size(), 1);
for (int i = 1; i < nums.size(); ++i)
{
for (int j = 0; j < i; ++j)
{
if (nums[i] > nums[j])
{
p[i] = max(p[i], q[j] + 1);
}
else if (nums[i] < nums[j])
{
q[i] = max(q[i], p[j] + 1);
}
}
}
return max(p.back(), q.back());
}
};
参考:https://www.cnblogs.com/grandyang/p/5697621.html
376 Wiggle Subsequence 摆动序列的更多相关文章
- LeetCode 376. Wiggle Subsequence 摆动子序列
原题 A sequence of numbers is called a wiggle sequence if the differences between successive numbers s ...
- 【LeetCode】376. Wiggle Subsequence 解题报告(Python)
[LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...
- Week13 - 376. Wiggle Subsequence
Week13 - 376. Wiggle Subsequence A sequence of numbers is called a wiggle sequence if the difference ...
- [LeetCode] Wiggle Subsequence 摆动子序列
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- Leetcode 376. Wiggle Subsequence
本题要求在O(n)时间内求解.用delta储存相邻两个数的差,如果相邻的两个delta不同负号,那么说明子序列摇摆了一次.参看下图的nums的plot.这个例子的答案是7.平的线段部分我们支取最左边的 ...
- 376. Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- 【Leetcode】376. Wiggle Subsequence
Description: A sequence of numbers is called a wiggle sequence if the differences between successive ...
- [Swift]LeetCode376. 摆动序列 | Wiggle Subsequence
A sequence of numbers is called a wiggle sequence if the differences between successive numbers stri ...
- Java实现 LeetCode 376 摆动序列
376. 摆动序列 如果连续数字之间的差严格地在正数和负数之间交替,则数字序列称为摆动序列.第一个差(如果存在的话)可能是正数或负数.少于两个元素的序列也是摆动序列. 例如, [1,7,4,9,2,5 ...
随机推荐
- Linux下汇编语言学习笔记27 ---
这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...
- 关于FFM (Field-aware Factorization Machines) 的几篇博客
深入FFM原理与实践 http://tech.meituan.com/deep-understanding-of-ffm-principles-and-practices.html#mjx-eqn-e ...
- Pagodas 等差数列
nn pagodas were standing erect in Hong Jue Si between the Niushou Mountain and the Yuntai Mountain, ...
- JSP中HTTP状态码
以下内容引用自http://wiki.jikexueyuan.com/project/jsp/http-status-codes.html: HTTP请求格式和HTTP响应消息的格式一样,都有以下结构 ...
- java代码判断文件类型(判断文件后缀名)
1.两点需要注意 1.string.spilt("\\.")分割字符串成子字符串数组,以“.”分割,必须写成string.spilt("\\.")的方式,不能写 ...
- HDU 4983 Goffi and GCD(数论)
HDU 4983 Goffi and GCD 思路:数论题.假设k为2和n为1.那么仅仅可能1种.其它的k > 2就是0种,那么事实上仅仅要考虑k = 1的情况了.k = 1的时候,枚举n的因子 ...
- Hadoop HDFS NFS GateWay部署深入具体解释
目的:通过挂载的方式,能够相似訪问本地磁盘的方式一样的訪问Hadoop文件.简单.方便.快捷. 0.系统版本号&hadoop版本号 1)系统版本号 [root@WEB-W031 sbin]# ...
- IOS View编程指南笔记
我们所示程序 对于一切IOS APP来说.我们看的的内容,都是UIView所呈现的. UIView如场景,UIWindow如舞台.UIView粉墨登场在UIWindow这个舞台上,使我们看到丰富多彩的 ...
- Android 4.4环境搭建——配置AVD模拟器
AVD(Android Virtual Device)即Android模拟器,它是Android官方提供的一个能够执行Android程序的虚拟机,在执行Android程序之前,首先须要创建AVD模拟器 ...
- ibatis 入门
iBatis 简单介绍: iBatis 是apache 的一个开源项目.一个O/R Mapping 解决方式,iBatis 最大的特点就是小巧.上手非常快.假设不须要太多复杂的功能.iBatis ...