当前位置:网站首页>LeetCode_ 376_ Wobble sequence
LeetCode_ 376_ Wobble sequence
2022-07-23 21:11:00 【Fitz1318】
Topic link
Title Description
If the difference between consecutive numbers strictly alternates between positive and negative numbers , Then the sequence of numbers is called Swing sequence . First difference ( If it exists ) It could be positive or negative . A sequence with only one element or two unequal elements is also regarded as a swing sequence .
for example , [1, 7, 4, 9, 2, 5] It's a Swing sequence , 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 .
Subsequence You can delete some... From the original sequence ( It can also be done without deleting ) Element to get , The remaining elements keep their original order .
Give you an array of integers nums , return nums As Swing sequence Of The length of the longest subsequence .
Example 1:
Input :nums = [1,7,4,9,2,5]
Output :6
explain : The whole sequence is a swing sequence , The difference between the elements is (6, -3, 5, -7, 3) .
Example 2:
Input :nums = [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 is [1, 17, 10, 13, 10, 16, 8] , The difference between the elements is (16, -7, 3, -3, 6, -8) .
Example 3:
Input :nums = [1,2,3,4,5,6,7,8,9]
Output :2
Tips :
1 <= nums.length <= 10000 <= nums[i] <= 1000
Advanced : Can you use O(n) Time complexity to complete this question ?
Their thinking
The law of greed
Take... For example 2 give an example , As shown in the figure below

Local optimum : Delete nodes on monotonic slopes ( Nodes at both ends of monotonic slope are not included ), Then this slope can have two local peaks
The best in the world : The whole sequence has the most local peaks , So as to achieve the longest swing sequence
problem : If you calculate the number of peaks by statistical difference, you need to consider the special cases of the leftmost and rightmost sides of the array , So for the sequence [2,5], We can assume that [2,2,5], So it has a slope , As shown in the figure below

AC Code
class Solution {
public int wiggleMaxLength(int[] nums) {
if (nums.length <= 1) {
return nums.length;
}
int ans = 1;// Number of peaks , The default sequence has a peak on the far right
int curDiff = 0;// The difference between the current pair
int preDiff = 0;// The difference between the previous pair
for (int i = 0; i < nums.length - 1; i++) {
curDiff = nums[i + 1] - nums[i];
// Peak appears
if ((curDiff > 0 && preDiff <= 0) || (preDiff >= 0 && curDiff < 0)) {
ans++;
preDiff = curDiff;
}
}
return ans;
}
}
边栏推荐
- Trial record of ModelBox end cloud collaborative AI development kit (rk3568) (II)
- 1063 Set Similarity
- 深入浅出边缘云 | 1. 概述
- High numbers | calculation of double integral 4 | high numbers | handwritten notes
- 【攻防世界WEB】难度四星12分进阶题:Confusion1
- 对接湖南CA使用U_KEY登录
- Green Tao theorem (4): energy increment method
- 1062 Talent and Virtue
- From which dimensions can we judge the quality of code? How to have the ability to write high-quality code?
- The third slam Technology Forum - Professor wuyihong
猜你喜欢

大三实习生,字节跳动面经分享,已拿Offer

MySql的DDL和DML和DQL的基本语法

221. 最大正方形 ●● & 1277. 统计全为 1 的正方形子矩阵 ●●

1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试

Himawari-8 data introduction and download method

High numbers | calculation of triple integral 2 | high numbers | handwritten notes

The common interfaces of Alipay are uniformly encapsulated and can be used directly for payment parameters (applicable to H5, PC, APP)

Vite3 learning records

网络学习型红外模块,8路发射独立控制

2022-7-23 12点 程序爱生活 小时线顶背离出现,保持下跌趋势,等待反弹信号出现。
随机推荐
Addon plugin 003 for CDR plugin development - awareness solution (SLN) and project (csproj) files
SQLite database
高数下|二重积分的计算3|高数叔|手写笔记
Edge cloud | 1. overview
1309_STM32F103上增加GPIO的翻转并用FreeRTOS调度测试
ssm+mysql实现零食商城系统(电商购物)
Read the five flow indicators of R & D efficiency insight
【持续更新】树莓派启动与故障系列集锦
Tropomi (sentinel 5p) data introduction and download method
TCP half connection queue and full connection queue (the most complete in History)
Too complete, it is recommended to collect! What can sap bring to the enterprise?
Identify some positions in the parenthesis sequence
Addon plug-in 002 of CDR plug-in development - write an EXE program that can be run by double clicking in 1 minute
Now I don't know how to synchronize at all
ES6特性:Promise(自定义封装)
[wechat applet] do you know about applet development?
When we talk about Chen Chunhua and Huawei, what are we talking about?
Chapter1 数据清洗
[100 cases of scratch drawing] Figure 46-scratch drawing flowers children's programming scratch programming drawing case tutorial grade examination competition drawing training case
jsp+ssm+mysql实现的租车车辆管理系统汽车租赁