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,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: 2
Example 3:
Input: [100]
Output: 1
Idea 1. Extending to a new element in the array. How to extend the solution from A[0...i] to A[0,...i, i+1]? We care about if the comparison sign has been alternated. The new length ending at i could be determined by the length ending at i-1 and the sign alternation.
len(i) = 1 if A[i-1] == A[i]
len(i) = 2 if A[i-1] != A[i] and comparison sign has not been alternated, i.e. previousSign * currentSign != -1
len(i) = len(i-1) + 1 if previousSign * currentSign == -1
Time complexity: O(n)
Space complexity: O(1)
class Solution {
public int maxTurbulenceSize(int[] A) {
int sign = 0;
int len = 1;
int maxLen = 1;
for(int i = 1; i< A.length; ++i) {
int currSign = Integer.compare(A[i-1], A[i]);
if(currSign != 0) {
if(sign * currSign != -1) {
len = 2;
}
else {
++len;
}
}
sign = currSign;
maxLen = Math.max(maxLen, len);
}
return maxLen;
}
}
1.b. save an extra variable, since we care only the consecutive comparison, 3 elements can determin if the sign has been alternated.
class Solution {
public int maxTurbulenceSize(int[] A) {
int len = 0;
int maxLen = 1;
for(int i = 0; i< A.length; ++i) {
if(i >= 2 && (Integer.compare(A[i-2], A[i-1]) * Integer.compare(A[i-1], A[i]) == -1)) {
++len;
}
else if(i >=1 && A[i-1] != A[i]) {
len = 2;
}
else {
len = 1;
}
maxLen = Math.max(maxLen, len);
}
return maxLen;
}
}
Idea 1.c recording length of the alternating block ending at i, the last two elements is either increasing or decreasing:
inc = dec + 1 if A[i] > A[i-1], reset dec = 1
dec = inc + 1 if A[i] < A[i-1], reset inc = 1
dec =1 , inc = 1 if A[i] == A[i-1] or i == 0
class Solution {
public int maxTurbulenceSize(int[] A) {
int dec = 0;
int inc = 0;
int maxLen = 0;
for(int i = 0; i< A.length; ++i) {
if(i== 0 || A[i-1] == A[i]) {
dec = 1;
inc = 1;
}
else if(A[i-1] < A[i]) {
inc = dec + 1;
dec = 1;
}
else {
dec = inc + 1;
inc = 1;
}
maxLen = Math.max(maxLen, Math.max(dec, inc));
}
return maxLen;
}
}
Idea 2. Sliding window, recording the potential starting point of the alternative block, once the block stop flipping the sign or reaching the end of the array, then caculate the length of the block. This idea reminds me the subarray min sum which find the previous smallest element and the next smallest element to find the length of block which contains larger elements.
start = i if A[i-1] == A[i]
len = i - start + 1 if i == nums.lengh -1 or Integer.compare(A[i-1], A[i]) == Integer.compare(A[i], A[i+1])
class Solution {
public int maxTurbulenceSize(int[] A) {
int start = 0;
int maxLen = 1;
for(int i = 1; i< A.length; ++i) {
int sign = Integer.compare(A[i-1], A[i]);
if(sign == 0) {
start = i;
}
else if(i == A.length-1 || sign * Integer.compare(A[i], A[i+1]) != -1) {
int len = i - start + 1;
maxLen = Math.max(maxLen, len);
start = i;
}
}
return maxLen;
}
}
Note:
1 <= A.length <= 400000 <= A[i] <= 10^9
Longest Turbulent Subarray LT978的更多相关文章
- 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], ..., ...
- [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 ...
- 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 ...
- 【LeetCode】978. Longest Turbulent Subarray 解题报告(C++)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 虫取法 日期 题目地址:https://leetco ...
- 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 ...
- Monotonic Array LT896
An array is monotonic if it is either monotone increasing or monotone decreasing. An array A is mono ...
- [LeetCode] Maximum Subarray 最大子数组
Find the contiguous subarray within an array (containing at least one number) which has the largest ...
随机推荐
- 配置完php.ini中的扩展库后,重启apache出现错误1067
网上有很多解决办法,比如更改环境变量,重装apache等等,但没有一个是符合我的.最后发现只是犯了一个低级错误,因为是第一次配置php.ini中的扩展库,忘记配置扩展库的路径. 解决办法:需要先加上扩 ...
- 二分图 最小点覆盖 poj 3041
题目链接:Asteroids - POJ 3041 - Virtual Judge https://vjudge.net/problem/POJ-3041 第一行输入一个n和一个m表示在n*n的网格 ...
- 11. Container With Most Water(头尾双指针)
Given n non-negative integers a1, a2, ..., an, where each represents a point at coordinate (i, ai). ...
- stm32中adc的常规通道和注入通道的区别
STM32的每个ADC模块通过内部的模拟多路开关,可以切换到不同的输入通道并进行转换.STM32特别地加入了多种成组转换的模式,可以由程序设置好之后,对多个模拟通道自动地进行逐个地采样转换. 有2种划 ...
- PHP简单工厂模式、工厂方法模式和抽象工厂模式比较
PHP工厂模式概念:工厂模式是一种类,它具有为您创建对象的某些方法.您可以使用工厂类创建对象,而不直接使用 new.这样,如果您想要更改所创建的对象类型,只需更改该工厂即可.使用该工厂的所有代码会自动 ...
- linux命令学习之:sed
sed:Stream Editor文本流编辑,sed是一个“非交互式的”面向字符流的编辑器.能同时处理多个文件多行的内容,可以不对原文件改动,把整个文件输入到屏幕,可以把只匹配到模式的内容输入到屏幕上 ...
- Exception starting filter encodingFilter
1. maven运行tomcat6出现错误Exception starting filter encodingFilter: 严重: Exception starting filter encodin ...
- windows下忘记mysql超级管理员root密码的解决办法(也适用于wamp)
1.停止mysql服务. 2,在CMD命令行窗口,进入MYSQL安装目录 比如 d:mysql20080505in 3,进入mysql安全模式,即当mysql起来后,不用输入密码就能进入数据库.命令为 ...
- Java面试基础知识(2)
1.一个".java"源文件中是否可以包括多个类(不是内部类)?有什么限制? 可以有多个类,但只能有一个public的类,并且public的类名必须与文件名相一致. 2.说说& ...
- how2j网站前端项目——天猫前端(第一次)学习笔记8
其他页面的学习 这些页面有1.查询结果页 2.支付页面 3.支付成功页面 4.确认收货页面上 5.确认收货页面下 6.收获成功页面 7.评价页面上 8.评价页面下 9.登陆页面 10.注册页面 1.查 ...