原题链接在这里: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 < jA[k] > A[k+1] when k is odd, and A[k] < A[k+1] when k is even;
  • OR, for i <= k < jA[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

题解:

Set some small examples like [1, 3, 2], [2,2] and find routine.

It matters the last 3 componenets. If it is l<m>r or l>m<r relationship, then length+1. Otherwise, reset to 2 or 1.

Let dp[i] denotes up to A[i-1], the longest turbulent length.

If  A[i-3]<A[i-2]>A[i-1] or  A[i-3]>A[i-2]<A[i-1], dp[i] = dp[i-1] + 1.

Maintain the maximum to res.

Time Complexity: O(n). n = A.length.

Space: O(n).

AC Java:

 class Solution {
public int maxTurbulenceSize(int[] A) {
if(A == null){
return 0;
} if(A.length < 2){
return A.length;
} int len = A.length;
int [] dp = new int[len+1];
dp[1] = 1;
dp[2] = A[0] == A[1] ? 1 : 2; int res = dp[2];
for(int i = 3; i<=len; i++){
if(A[i-2]<A[i-3] && A[i-2]<A[i-1] || A[i-2]>A[i-3] && A[i-2]>A[i-1]){
dp[i] = dp[i-1] + 1;
res = Math.max(res, dp[i]);
}else if(A[i-1] == A[i-2]){
dp[i] = 1;
}else{
dp[i] = 2;
}
} return res;
}
}

It only cares about dp[i-1]. Thus it could reduce dimension.

Time Complexity: O(n).

Space: O(1).

AC Java:

 class Solution {
public int maxTurbulenceSize(int[] A) {
if(A == null){
return 0;
} if(A.length < 2){
return A.length;
} int len = A.length;
int dp = A[0] == A[1] ? 1 : 2;
int res = dp; for(int i = 3; i<=len; i++){
if(A[i-2]<A[i-3] && A[i-2]<A[i-1] || A[i-2]>A[i-3] && A[i-2]>A[i-1]){
dp = dp + 1;
res = Math.max(res, dp);
}else if(A[i-1] == A[i-2]){
dp = 1;
}else{
dp = 2;
}
} return res;
}
}

类似Maximum Subarray.

LeetCode 978. Longest Turbulent Subarray的更多相关文章

  1. 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 ...

  2. 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...

  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. [Swift]LeetCode978. 最长湍流子数组 | 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 ...

  5. Longest Turbulent Subarray LT978

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

  6. [LeetCode] 674. Longest Continuous Increasing Subsequence_Easy Dynamic Programming

    Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...

  7. [LeetCode] 325. Maximum Size Subarray Sum Equals k 和等于k的最长子数组

    Given an array nums and a target value k, find the maximum length of a subarray that sums to k. If t ...

  8. 1438. Longest Continuous Subarray With Absolute Diff Less Than or Equal to Limit

    Given an array of integers nums and an integer limit, return the size of the longest continuous suba ...

  9. LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法

    LeetCode(4) || Longest Palindromic Substring 与 Manacher 线性算法 题记 本文是LeetCode题库的第五题,没想到做这些题的速度会这么慢,工作之 ...

随机推荐

  1. PHP 使用 pdo 操作oracle数据库 报错

    ## SELECT UNID,NAME,NAME_XML WHERE UNID>=10 AND UNID<=15 ## 在10到15这5条数据中不为空数据php: symbol looku ...

  2. linux下nginx部署以及配置详解

    1.下载源码包解压编译 启动多个,请看:在linux系统下安装两个nginx以及启动 查看nginx包路径:http://nginx.org/download/,两种下载方式: 1.在官网下载使用Xf ...

  3. DEDE5.5招聘模板

    <channel:id>18</channel:id> <channel:nid>zhaopin</channel:nid> <channel:t ...

  4. [golang]按图片中心旋转后的新图左顶点和原图左顶点的偏移量计算

    1 前言 略,作为记录使用 2 代码 /** * @Author: FB * @Description: * @File: RotateSample.go * @Version: 1.0.0 * @D ...

  5. .net core 使用swagger接口描述

    首先安装nuget包 Swashbuckle.AspNetCore.Swagger Swashbuckle.AspNetCore.SwaggerGen Swashbuckle.AspNetCore.S ...

  6. JavaScript_proto_和prototype到底是什么玩意

    _proto_和prototype到底有什么区别啊?是个什么东西啊? 在这里我头也比较大啊,小学语文没学好,所以组织能力比较差劲,所以尽量的咱用代码来解释吧. function too() { thi ...

  7. JavaScript学习笔记(6月份)

    由于笔记比较杂,本身学习程度并不理想,所以暂时没有整理这些繁杂的笔记. ps:博客园markdown用起来和看起来都舒服太多了,这才是我了解的那个markdown,又回来了! 笔记 DOM对象 doc ...

  8. JavaScript之鼠标事件

    事件三要素: 事件源.事件类型(点击onclick)=function(){ 事件触发后执行的代码 } 案例: function abb(a){ return document.getElementB ...

  9. 英语Petrolaeum原油

    Petrolaeum (英语单词) Petrolaeum是一个英语单词,名词,翻译为石油. 中文名:石油 外文名:petrolaeum,petroleum 目录 1 含义 2 例句 含义 petrol ...

  10. Mysql慢查询日志以及优化

    慢查询日志设置 当语句执行时间较长时,通过日志的方式进行记录,这种方式就是慢查询的日志. 1.临时开启慢查询日志(如果需要长时间开启,则需要更改mysql配置文件) set global slow_q ...