当前位置:网站首页>leetcode 376. Wiggle Subsequence
leetcode 376. Wiggle Subsequence
2022-07-28 12:46:00 【.DoubleBean.】
Title address (376. Swing sequence )
https://leetcode-cn.com/problems/wiggle-subsequence/
Title Description
If the difference between consecutive numbers strictly alternates between positive and negative numbers , Then the number sequence is called the swing sequence . First difference ( If it exists ) It could be positive or negative . A sequence of less than two elements is also a wobble sequence .
for example , [1,7,4,9,2,5] It's a sequence of oscillations , Because of the difference (6,-3,5,-7,3) It's alternating positive and negative . contrary , [1,4,7,2,5] and [1,7,4,5,5] It's not a wobble sequence , The first sequence is because its first two differences are positive , The second sequence is because its last difference is zero .
Given a sequence of integers , Returns the length of the longest subsequence as a swing sequence . By removing some... From the original sequence ( It can also be done without deleting ) Element to get the subsequence , The remaining elements keep their original order .
Example 1:
Input : [1,7,4,9,2,5]
Output : 6
explain : The whole sequence is a swing sequence .
Example 2:
Input : [1,17,5,10,13,15,10,5,16,8]
Output : 7
explain : This sequence contains several lengths of 7 Swing sequence , One of them can be [1,17,10,13,10,16,8].
Example 3:
Input : [1,2,3,4,5,6,7,8,9]
Output : 2
Advanced :
Can you use O(n) Time complexity to complete this question ?
Paste below leetcode The solution of the great God , In order to look back later
Paste here the explanation of a great God in the solution to the dynamic programming code .
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
int i, down = 1, up = 1, len = nums.size();
for(i=1;i<len;i++){
if(nums[i]>nums[i-1]){
up = down + 1;
}
else if(nums[i]<nums[i-1]){
down = up + 1;
}
}
return len == 0 ? 0 : max(up, down);
//len == 0( Empty list []) When to return to 0,len == 1( That is, an element [3]) When to return to 1
}
};
Paste it here leetcode Official explanation
Both ends of the sequence must be peaks or valleys , If the first two of the sequence are the same data , There is only one front end , therefore prevdiff=1, If the first two of the sequence are different data , That may be ascending or descending , Then add this situation ,prevdiff In the beginning, it was 2
// Greedy Algorithm
class Solution {
public:
int wiggleMaxLength(vector<int>& nums) {
int n = nums.size();
if (n < 2) {
return n;
}
int prevdiff = nums[1] - nums[0];
int ret = prevdiff != 0 ? 2 : 1;
for (int i = 2; i < n; i++) {
int diff = nums[i] - nums[i - 1];
if ((diff > 0 && prevdiff <= 0) || (diff < 0 && prevdiff >= 0)) {
ret++;
prevdiff = diff;
}
}
return ret;
}
};
边栏推荐
- Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
- 1331. Array sequence number conversion: simple simulation question
- Open source huizhichuang future | 2022 open atom global open source summit openatom openeuler sub forum was successfully held
- Under what circumstances can the company dismiss employees
- MarkDown简明语法手册
- 遭受痛苦和创伤后的四种本真姿态 齐泽克
- 开源汇智创未来 | 2022 开放原子全球开源峰会 OpenAtom openEuler 分论坛圆满召开
- SuperMap game engine license module division
- [nuxt 3] (XII) project directory structure 3
- Kafaka丢消息吗
猜你喜欢

Zadig v1.13.0 believes in the power of openness, and workflow connects all values

Siemens docking Leuze BPS_ 304i notes

Kafaka丢消息吗

01 pyechars 特性、版本、安装介绍

Multiple items on a computer share a public-private key pair to pull the Gerrit server code

Initialization examples of several modes of mma8452q

Sub database and sub table may not be suitable for your system. Let's talk about how to choose sub database and sub table and newsql

Hc-05 Bluetooth module debugging slave mode and master mode experience

VS1003调试例程

试用copilot过程中问题解决
随机推荐
Yan Ji lost Beijing again, and more than half of the stores in the country were closed
VS1003调试例程
【Base】优化性能到底在优化啥?
1331. Array sequence number conversion: simple simulation question
设计一个线程池
Redis implements distributed locks
SuperMap itablet license module division
Markdown concise grammar manual
连通块&&食物链——(并查集小结)
[half understood] zero value copy
Leetcode: array
LeetCode 移除元素&移动零
Insufficient permission to pull server code through Jenkins and other precautions
GMT安装与使用
Uncover why devaxpress WinForms, an interface control, discards the popular maskbox property
Distributed session solution
Did kafaka lose the message
Custom paging tag 02 of JSP custom tag
界面控件Telerik UI for WPF - 如何使用RadSpreadsheet记录或评论
Fastjson parses multi-level JSON strings