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 ...
随机推荐
- 289. Game of Live
According to the Wikipedia's article: "The Game of Life, also known simply as Life, is a cellul ...
- 单调队列&单调栈 基础
参考博客https://www.cnblogs.com/tham/p/8038828.html 例题 poj 2823 Sliding Window Time Limit: 12000MS Me ...
- Sum-(最大子序列和)
http://bestcoder.hdu.edu.cn/contests/contest_showproblem.php?cid=652&pid=1003 题目大意: 给你一个序列,你随便找一 ...
- springboot整体介绍
1.springboot:快速开发,强大的运维能力.(监控,服务发现,并打) 2.微服务,将一个大系统分解成很多独立的小服务,这些服务能随时发布. 3.2004年第一版spring 1.0,rod j ...
- mingw32-gcc.exe: error: CreateProcess: No such file or directory
用code::blocks在windows平台下,搭建object c编译环境时,出现这个错误. 解决的方法: 将setting -> compliler -> Toolchain exe ...
- YII数据流程浅析
MVC就不解释,直接上代码分析数据流程: 数据库图: 模型部分介绍: <?php /* * 前两个方法必须写 * 继承自CActiveRecord类 这个类位于 \framework\db\ar ...
- node+vue-cli+webpack搭建教程
链接:https://segmentfault.com/a/1190000009160934?_ea=1849098 链接:https://www.jianshu.com/p/2769efeaa10a ...
- Angularjs1.x 项目结构
大部分的项目结构是以 directives , service, controller 为基础来搭建的项目架构的,但这里更偏向于以应用场景来进行项目架构,因此这里的文件夹结构可能与您之前遇到的结构不同 ...
- mac终端配置Android ADB命令
不得不说mac是一款开发利器,不仅可以开发ios,而且对于Android开发也是不错的选择,下面我就对mac配置adb命令,进行简要的说明.下面我将一下mac环境下的配置步骤:1.在自己的目录(hom ...
- nRF52832之硬件I2C
这几天一直在折腾nRF52832的硬件I2C,到了今天最终出现了成果,在此也印证了那句话:"耕耘就有收获" 52832的硬件I2C尽管官方提供了demo,可是自己对I2C通信理解的 ...