978. Longest Turbulent Subarray
A subarray
A[i], A[i+1], ..., A[j]ofAis said to be turbulent if and only if:
- For
i <= k < j,A[k] > A[k+1]whenkis odd, andA[k] < A[k+1]whenkis even;- OR, for
i <= k < j,A[k] > A[k+1]whenkis even, andA[k] < A[k+1]whenkis 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: 2Example 3:
Input: [100]
Output: 1
Note:
1 <= A.length <= 400000 <= 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的更多相关文章
- 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 ...
- LeetCode 978. Longest Turbulent Subarray
原题链接在这里:https://leetcode.com/problems/longest-turbulent-subarray/ 题目: A subarray A[i], A[i+1], ..., ...
- 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...
- [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 ...
- 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 ...
- 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 ...
- 【LeetCode】1438. 绝对差不超过限制的最长连续子数组 Longest Continuous Subarray With Absolute Diff Less Than or Equal t
作者: 负雪明烛 id: fuxuemingzhu 个人博客:http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 滑动窗口 日期 题目地址:https://leetco ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
- [LeetCode] 53. Maximum Subarray 最大子数组
Given an integer array nums, find the contiguous subarray (containing at least one number) which has ...
随机推荐
- jmeter完成数据批量添加
Jmeter结构如图 目的: 需要在每个组织下面分别添加5个设备资源 思路: 1.先登录平台 2.进入系统配置页面 3.获取到每个区域的ID 4.在每个区域下面添加设备资源 重点及难点: 1.登录加密 ...
- abort: no username supplied (see "hg help config")
abort: no username supplied (see "hg help config") 在hg中输入commit 指令时,如果出现下述结果: $ hg commit ...
- Laravel 开启跨域请求
项目中用到了接口,外部调用的时候老是请求不到,本地请求却没问题,查了下说是因为跨域的问题.根据网上所说解决方法如下: 1.建立中间件Cors.php命令:php artisan make:middle ...
- 《JavaScript DOM编程艺术》笔记
1. 把<script>标签放到HTML文档的最后,<body>标签之前能使浏览器更快地加载页面. 2. nodeType的常见取值 元素节点(1) 属性节点(2) 文本节点( ...
- js实现a_b变成A B的两种方法
1.var key = 'a_b'; var a = key.replace(/\b.|_./g, function (i) { if (i.length === 2) { i = ' ' + i[1 ...
- Django入门与实践-第20章:QuerySets(查询结果集)(完结)
http://127.0.0.1:8000/boards/1/ #boards/models.py from django.utils.text import Truncator class Topi ...
- excel绝对引用中间添加符号
=F1&"_"&J1 绝对引用 相对引用 按F4 然后复制全部,选择性黏贴,值和数字即可
- 对比手机SLAM和机器人SLAM
陀螺仪?? IMU?? 加速度器与?? 人与机器 惯性定位与?? 步骤上对比Project Tango与SLAM
- 20145234黄斐《java程序设计》第六周
教材学习内容总结 第十章:输入与输出 InputStream与OutputStream 流(Stream)是对「输入输出」的抽象,注意「输入输出」是相对程序而言的 InputStream与Output ...
- Scala包和引用
1.包 Scala包的命名方式有两种.一种和Java一样,通过把package子句放在文件顶端的方式,把整个文件的内容放进包里.如: package scala.actors.Actor 另一种方式可 ...