原题链接在这里: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. Vue 动态路由的实现以及 Springsecurity 按钮级别的权限控制

    思路: 动态路由实现:在导航守卫中判断用户是否有用户信息,通过调用接口,拿到后台根据用户角色生成的菜单树,格式化菜单树结构信息并递归生成层级路由表并使用Vuex保存,通过 router.addRout ...

  2. 「UR#6」懒癌

    「UR#6」懒癌 妈妈我居然看了六个小时题解,快救救乌干达的可怜儿童吧. 接下来开始膜官方题解: ​ 其实就算有上面两个结论也不是很好想到任意复杂度的做法,关键在于要想到一个人是怎么推断自己的狗是不是 ...

  3. Maven 初学+http://mvnrepository.com/

    了解 maven是一款服务于java平台的自动化构建工具(项目管理工具) 构建:全方位.多角度.深层次地建立 项目构建是一个项目从:源代码.编译.测试.打包.部署.运行的过程 用来解决团队开发遇到的问 ...

  4. .NET Core入门

            .Net core MVC   如何使用 .NET Core,最基本的入行,很多博客以及官网都有的太多太多的例子,但是大部分没有人做到了真的让一个小白一步一步的去学, 我第一次接触的时 ...

  5. Git查看文件制定行区间的提交记录

    git blame -L , /dir/file/file.php 这里查看file文件下6610至6613行的修改记录

  6. Matlab工厂模式

    工厂模式定义一个用于创建对象的接口,让子类决定实例化哪一个类,工厂方法使一个类的实例化延迟到其子类.工厂模式主要分为简单工厂模式.工厂方法模式以及抽象工厂模式. Obj.m classdef Obj ...

  7. JavaScript insertAdjacentHTML()的使用

    含义: insertAdjacentHTML() 将指定的文本解析为HTML或XML,并将结果节点插入到DOM树中的指定位置.它不会重新解析它正在使用的元素,因此它不会破坏元素内的现有元素.这避免了额 ...

  8. 网络监听工具 嗅探器 SpyNet

    配置网卡 注册 监听配置 开始捕获

  9. 一道经典面试题,atoi函数的实现

    参考资料 (1)atoi函数的实现 (2)<剑指offer> 题目分析 本题需要注意的有几个方面: (1)检查输入参数,指针是否为NULL: (2)去除字符串前面的空格 (3)处理正负符号 ...

  10. PHP Lumen Laravel 解决validate方法自定义message无效的问题

    /** * 由于 \Laravel\Lumen\Routing\ProvidesConvenienceMethods::validate 在验证不通过时, * 抛出 \Illuminate\Valid ...