原题链接在这里: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. 第十届蓝桥杯大赛-特别数的和-C++

    解法一(暴力获取): #include<stdio.h> #include<stdlib.h> int main(void) { int n; ; ; printf(" ...

  2. 不会前后端,用vps搭建个人博客(一)

    一.vps供应商选择 常见的国内有腾讯云(良心云).阿里云(套路云)等,国外有bandwagon和vultr,本人选的vultr山姆叔叔东部便宜小鸡.目前vlutr还有新用户注册后充值10刀送50刀的 ...

  3. gRPC-拦截器简单使用

    概述 gRPC作为通用RPC框架,内置了拦截器功能.包括服务器端的拦截器和客户端拦截器,使用上大同小异.主要作用是在rpc调用的前后进行额外处理. 从客户端角度讲,可以在请求发起前,截取到请求参数并修 ...

  4. jupyter notebook在 mac 使用

    1. 查看当前 conda 所拥有的环境列表 conda env list 2. 选择要进入的环境 source activate your_env_name 3. 启动 jupyter jupyte ...

  5. Scala 系列(七)—— 常用集合类型之 Map & Tuple

    一.映射(Map) 1.1 构造Map // 初始化一个空 map val scores01 = new HashMap[String, Int] // 从指定的值初始化 Map(方式一) val s ...

  6. windows操作系统更改 <远程桌面> 端口号

    windows远程桌面连接默认使用的是3389端口,为了避免被他人扫描从而暴力破解远程服务器或者病毒入侵.可以将默认端口修改为其它端口,如8888,11111等.最好修改为10000以后的端口,这样可 ...

  7. C#下IOC/依赖注入框架Grace介绍

    对依赖注入或控制反转不了解的童鞋请先自行学习一下这一设计,这里直接介绍项目和实现步骤. Grace是一个开源.轻巧.易用同时特性丰富.性能优秀的依赖注入容器框架.从这篇IOC容器评测文章找到的Grac ...

  8. js 时间常用处理方法

    众所周知,JavaScript核心包含Data()构造函数,用来创建表示时间和日期的对象. 今天主要跟大家梳理一下,常用的时间.日期处理方法,方便大家使用和理解 格式化时间 老生常谈,大概会这么写 1 ...

  9. 使用cmd命令行登录mysql并查看mysql状态

    直接上代码,打开cmd命令窗口,进入mysql的安装目录(例如:cd  D:/lnmp/bin/mysql/mysql5.7.11/bin)输入: #mysql -u root -p Enter pa ...

  10. misc_register杂项设备

    include/linux/miscdevice.h 这些字符设备不符合预先确定的字符设备范畴 设备主设备号10 struct miscdevice { int minor; //次设备号(如果设置为 ...