作者: 负雪明烛
id: fuxuemingzhu
个人博客: http://fuxuemingzhu.cn/


题目地址:https://leetcode.com/problems/longest-turbulent-subarray/

题目描述

A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if:

  • For i <= k < j, A[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;
  • OR, for i <= k < j, A[k] > A[k+1] when k is even, and A[k] < A[k+1] when k is odd.
    That is, the subarray is turbulent if the comparison sign flips between each adjacent pair of elements in the subarray.

Return the length of a maximum size turbulent subarray of A.

Example 1:

Input: [9,4,2,10,7,8,8,1,9]
Output: 5
Explanation: (A[1] > A[2] < A[3] > A[4] < A[5])

Example 2:

Input: [4,8,12,16]
Output: 2

Example 3:

Input: [100]
Output: 1

Note:

  1. 1 <= A.length <= 40000
  2. 0 <= A[i] <= 10^9

题目大意

如果相邻的两个数字的差在大于0和小于0这两种情况中互相翻转,那么说明这个子数组是符合要求的。求最长的符合要求的子数组。

解题方法

虫取法

虫取法就是前后指针交替前进的方式。

对于这个题来说,我们先固定左侧的指针left,然后把右侧指针right向右移动,我使用了isde来表示上一次的相邻数字的差的符号,每次把当前的相邻数字符号和上一次的进行判断。需要注意的是题目要求必须翻转,如果连续数字是相等的则不符合要求。

移动的时候需要注意是不是相邻数字的差交替的:

如果是交替的,那么更新现在的相邻数字差的符号并且更新最长子串的长度;

如果不是交替的,那么需要更新left指针,这里的不是交替有两个情况,一是相等,二是连续的递增或者相减。如果right指向的元素是连续两个相等的,那么left指向right当前的位置当做新的起点;如果是A[right - 2],A[right - 1],A[right]三者有序的情况,那么把left更新到right-1的位置,此时right不要向右移动,相当于把left = right - 1当做新的数组起始位置。

举个栗子:

[9,4,2,10,7,8,8,1,9]

9,4,2是单调递减的三个数字,此时应该把left移动到4的位置,right仍然指向2,开始新的遍历过程。

当来回翻转,right到了第二个8的时候,此时子数组是4,2,10,7,8,8,已经不满足翻转条件,此时把left指向right为第二个8的位置,然后开始新的遍历过程。

再举个栗子:

[1,1,1,1,1,1,1]

这样都是连续相等的数值,那么A[right] == A[right - 1]会一直成立,所以每次都把left更新到right的位置,则没有一个构成翻转的条件,res不更新。由此也可见,应该把满足要求的最小的子数组的长度res初始化为1.

c++代码如下:

class Solution {
public:
int maxTurbulenceSize(vector<int>& A) {
const int N = A.size();
if (N == 1) return 1;
int res = 1;
int left = 0, right = 1;
bool isde = false;
while (right < N) {
if (A[right] == A[right - 1]) {
left = right;
right++;
} else if (right - left == 1 || (A[right] - A[right - 1] < 0 != isde)) {
isde = A[right] - A[right - 1] < 0;
res = max(res, right - left + 1);
right ++;
} else {
left = right - 1;
}
}
return res;
}
};

日期

2019 年 1 月 20 日 —— 这次周赛有点简单

【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)的更多相关文章

  1. LeetCode 978. Longest Turbulent Subarray

    原题链接在这里:https://leetcode.com/problems/longest-turbulent-subarray/ 题目: A subarray A[i], A[i+1], ..., ...

  2. leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)

    传送门:点我 978. Longest Turbulent Subarray A subarray A[i], A[i+1], ..., A[j] of A is said to be turbule ...

  3. 978. Longest Turbulent Subarray

    A subarray A[i], A[i+1], ..., A[j] of A is said to be turbulent if and only if: For i <= k < j ...

  4. 【LeetCode】Longest Palindromic Substring 解题报告

    DP.KMP什么的都太高大上了.自己想了个朴素的遍历方法. [题目] Given a string S, find the longest palindromic substring in S. Yo ...

  5. 【LeetCode】718. Maximum Length of Repeated Subarray 解题报告(Python)

    [LeetCode]718. Maximum Length of Repeated Subarray 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: fuxu ...

  6. 【LeetCode】873. Length of Longest Fibonacci Subsequence 解题报告(Python)

    [LeetCode]873. Length of Longest Fibonacci Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 作者: 负雪明烛 id: ...

  7. 【LeetCode】673. Number of Longest Increasing Subsequence 解题报告(Python)

    [LeetCode]673. Number of Longest Increasing Subsequence 解题报告(Python) 标签(空格分隔): LeetCode 题目地址:https:/ ...

  8. 【LeetCode】376. Wiggle Subsequence 解题报告(Python)

    [LeetCode]376. Wiggle Subsequence 解题报告(Python) 作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.c ...

  9. 【LeetCode】647. Palindromic Substrings 解题报告(Python)

    [LeetCode]647. Palindromic Substrings 解题报告(Python) 标签: LeetCode 题目地址:https://leetcode.com/problems/p ...

随机推荐

  1. Hive-删除表(drop、truncate的区别)

    Hive删除操作主要分为几大类:删除数据(保留表).删除库表.删除分区.我将以下图为例清空iot_devicelocation中的数据,之后再删除表.库等. 解释: use xpu123;   #使用 ...

  2. 28-Merge Two Sorted Lists

    easy 21. Merge Two Sorted Lists Merge two sorted linked lists and return it as a new list. The new l ...

  3. C++你不知道的事

    class A { public: A() { cout<<"A's constructor"<<endl; } virtual ~A() { cout&l ...

  4. mac系统升级导致VirtualBox报Kernel driver not installed (rc=-1908)

    一.背景 最近将我的Mac升级成了Monterey版本,结果发现之前的安装的VirtualBox虚拟机无法启动,报了如下错误. Kernel driver not installed (rc=-190 ...

  5. Spark3学习【基于Java】3. Spark-Sql常用API

    学习一门开源技术一般有两种入门方法,一种是去看官网文档,比如Getting Started - Spark 3.2.0 Documentation (apache.org),另一种是去看官网的例子,也 ...

  6. C/C++运行时确定字节顺序

    字节顺序(英文:Endianness),多字节数据在内存中的存储顺序: 1.对于特定数据,内存空间有起始地址.结束地址: 2.对于数据本身,存在高位字节.地位字节:例如 int data = 0x01 ...

  7. python-3.x-生成器使用

    生成器函数代码: 1 def gen(n): 2 i = 1; 3 sum = 0; 4 while i <= n: 5 ''' 6 方法体1 -- sum求和是1到9的和 7 yield su ...

  8. 日常Java 2021/10/21

    Java Iterator(迭代器) 如果需要使用iterator类需要从java.util包中引入它 Java Iterator不是一个集合,它是一种访问集合的方法,用于迭代ArrayList和Ha ...

  9. E面波导和H面波导的问题

    我感觉与窄壁平行是E面,反之为H面.通常E面(窄面)是指与电场方向平行的方向图切面(窄面):H面(宽面)是指与磁场方向平行的方向图切面(宽面).E面的意思是... ElevationH面的意思是... ...

  10. 【STM32】使用SDIO进行SD卡读写,包含文件管理FatFs(三)-SD卡的操作流程

    其他链接 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(一)-初步认识SD卡 [STM32]使用SDIO进行SD卡读写,包含文件管理FatFs(二)-了解SD总线,命令的相关介绍 ...