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

Approach #1: Math. [Java]

class Solution {
public int maxTurbulenceSize(int[] A) {
int len = A.length;
int inc = 1, dec = 1, result = 1;
for (int i = 1; i < len; ++i) {
if (A[i] < A[i-1]) {
dec = inc + 1;
inc = 1;
} else if (A[i] > A[i-1]) {
inc = dec + 1;
dec = 1;
} else {
inc = 1;
dec = 1;
} result = Math.max(result, Math.max(inc, dec));
} return result;
}
}

  

Analysis:

inc: denote the length of subarray with two increase elements;

dec: denote the length of subarray with two decrease elements;

Reference:

https://leetcode.com/problems/longest-turbulent-subarray/discuss/221935/Java-O(N)-time-O(1)-space

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

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

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

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

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

  7. 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t

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

  8. [LeetCode] Maximum Subarray 最大子数组

    Find the contiguous subarray within an array (containing at least one number) which has the largest ...

  9. [LeetCode] 53. Maximum Subarray 最大子数组

    Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...

随机推荐

  1. js 获取高度

    网页可见区域宽 :document.body.clientWidth; 网页可见区域高:document.body.clientHeight;   网页可见区域高:document.body.offs ...

  2. 四种强制类型转换的总结(const_cast、static_cast、dynamic_cast、reinterpreter_cast)

    四种强制类型转换的总结(const_cast.static_cast.dynamic_cast.reinterpreter_cast) 转载 2011年10月03日 23:59:05 标签: stru ...

  3. ajax访问当前页面后的 [WebMethod]描述的方法

    脚本: function show() { $.ajax({ type: "post", async: false, contentType: "application/ ...

  4. golang开发集训营

    初识golang Go与VScode开发工具 Golang开门见山

  5. [SoapUI] 设置HTTP Request的Header

    import com.eviware.soapui.support.types.StringToStringMap def hearderMap = new StringToStringMap() d ...

  6. mvc的表单发送ajax请求,太强大了!!!!

  7. 前端之JavaScript笔记1

    一 JavaScript的引入方式 <!DOCTYPE html> <html lang="en"> <head> <meta chars ...

  8. phalApi框架打印SQL语句

    http://demo.phalapi.net/?service=User.getBaseInfo&user_id=1&__sql__=1

  9. Django的路由层(2)

    https://www.cnblogs.com/yuanchenqi/articles/8931472.html django2.0版的path Django默认支持以下5个转化器: str,匹配除了 ...

  10. 开源投影工具Proj——进行坐标转换

    proj.4 is a standard UNIX filter function which converts geographic longitude and latitude coordinat ...