原题链接在这里: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. redis的redisvCommand的%b

    如下代码,向redis发送命令 SendCommand("HSET %b %b %b",key.data(),key.size(),filed.data(),filed.size( ...

  2. DELPHI6中DSGNINTF.DCU找不到时的解决方法

    https://www.cnblogs.com/gaodu2003/archive/2009/06/04/1495789.html 1.添加 lib\designide.dcp到控件的dpk文件的re ...

  3. VM配置Centos(第十三步分区设置)

    1.点击开启此虚拟机之后,选择第一个 (注意:如果鼠标不显示出来,按alt+ctrl键) 2.然后选择skip跳过检测,如果选择了ok就会有很长时间的检测 3.然后选择NEXT 4.选择中文,然后点击 ...

  4. cloudera cdh6.3 离线安装 经典大数据平台视频教程(含网盘下载地址)

    cdh6.3企业级大数据视频教程 链接:https://pan.baidu.com/s/1bLGrIwzpFQB-pQRb6KOmNg 提取码:i8h8 系统和软件版本1,操作系统:Centos7.6 ...

  5. 使用Powershell启用/关闭Windows功能

    接上一篇博客,依旧是需要制作安装包,需要开启系统的MSMQ功能.这里就用到了dism.exe DISM全称是Deployment Image Servicing and Management(部署映像 ...

  6. 两台Windows的 IIS 应用站点 基于NLB + ARR 实现双机热备和负载

    IIS负载均衡中我们使用微软的ARR,但提到网站的高可用性,ARR只能做请求入口的消息分发服务,这样如果我们的消息分发服务器给down掉啦,那么做再多的应用服务集群也都枉然. 这里我们主要针对解决这一 ...

  7. int转换为String,常用的四种方法。

    int i = 100; 方法一:String s1 = i + " "; 方法二:String s2 = String.valueof(i); 方法三(先转换为Integer类型 ...

  8. 关于zynq系列板卡设计VREFP_0参考电压的疑问及解答

    使用板卡:Z-turn Board 芯片:Xilinx Zynq-7010/7020处理器 有工程师在试用zynq系列Z-turn Board时提出:在原理图P3页 Bank0上VREFP_0端接地的 ...

  9. JavaScript之控制标签属性

    var pic=document.getElementById('pic'); var obtn=document.getElementById('btn'); console.log(pic.get ...

  10. 77.JS本地保存数据的几种方法

    1.Cookie 这个恐怕是最常见也是用得最多的技术了,也是比较古老的技术了.COOKIE优点很多,使用起来很方便 但它的缺点也很多: 比如跨域访问问题:无法保存太大的数据(最大仅为4KB):本地保存 ...