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 ...
 
随机推荐
- JDBC连接各种数据库的方法,连接MySql,Oracle数据库
			
JDBC连接各种数据库的方法: JDBC编程步骤: 1.导入jar包 2.注册驱动 3.获取数据库连接对象 4.定义SQL语句 5.获得执行SQL语句对象statemnet 6.执行SQL语句 7.处 ...
 - yum被锁定:Another app is currently holding the yum lock; waiting for it to exit…
			
yum被锁定无法使用,错误信息截图如下: 解决方法:rm -rf /var/run/yum.pid 来强行解除锁定,然后你的yum就可以运行了
 - metasploit framework(四):生成payload
			
RHOST是限制来源IP,这里不用修改 generate生成payload 假设'\x00'是坏字符,生成payload时就会对'\x00'进行编码,排除坏字符. -b 去掉坏字符 -t 指定生成的格 ...
 - H5 边框:带border的百分比布局
			
响应式Web设计经常需要我们通过百分比设置组件宽度.如果我们不考虑边框,那么很容易就可以实现,但如果你给每一列以及总宽度都采用百分比设置,那这个时候固定的边框大小就会出来捣乱.下面我们将看到一组方法去 ...
 - php苹果内购订单验证
			
/** * 21000 App Store不能读取你提供的JSON对象 * 21002 receipt-data域的数据有问题 * 21003 receipt无法通过验证 * 21004 提供的sha ...
 - Allowing GPU memory growth
			
By default, TensorFlow maps nearly all of the GPU memory of all GPUs (subject to CUDA_VISIBLE_DEVICE ...
 - TOJ 5225: 玩转二叉树
			
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=5225 时间限制(普通/Java): ...
 - 81. Search in Rotated Sorted Array II (Array; Divide-and-Conquer)
			
Follow up for "Search in Rotated Sorted Array":What if duplicates are allowed? Would this ...
 - JSON Extractor
			
JMeter处理responses 的json 对于请求1返回的结果,处理以后作为请求2的参数,JMeter提供了JSON 提取器 比如 responses 返回: {"statusCode ...
 - ASP.Net MVC 中EF实体的属性取消映射数据库、自定义名称
			
例如:数据库中一个字段名称为CompanyId 自定义实体数据名称 [Column("CompanyId")] public int Id{ get; set; } 这样就可以使用 ...