LeetCode 978. Longest Turbulent Subarray

Posted Dylan_Java_NYC

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了LeetCode 978. Longest Turbulent Subarray相关的知识,希望对你有一定的参考价值。

原题链接在这里: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:

 1 class Solution {
 2     public int maxTurbulenceSize(int[] A) {
 3         if(A == null){
 4             return 0;
 5         }
 6         
 7         if(A.length < 2){
 8             return A.length;
 9         }
10         
11         int len = A.length;
12         int [] dp = new int[len+1];
13         dp[1] = 1;
14         dp[2] = A[0] == A[1] ? 1 : 2;
15         
16         int res = dp[2];
17         for(int i = 3; i<=len; i++){
18             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]){
19                 dp[i] = dp[i-1] + 1;
20                 res = Math.max(res, dp[i]);
21             }else if(A[i-1] == A[i-2]){
22                 dp[i] = 1;
23             }else{
24                 dp[i] = 2;
25             }
26         }
27         
28         return res;
29     }
30 }

It only cares about dp[i-1]. Thus it could reduce dimension.

Time Complexity: O(n).

Space: O(1).

AC Java:

 1 class Solution {
 2     public int maxTurbulenceSize(int[] A) {
 3         if(A == null){
 4             return 0;
 5         }
 6         
 7         if(A.length < 2){
 8             return A.length;
 9         }
10         
11         int len = A.length;
12         int dp = A[0] == A[1] ? 1 : 2;
13         int res = dp;
14         
15         for(int i = 3; i<=len; i++){
16             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]){
17                 dp = dp + 1;
18                 res = Math.max(res, dp);
19             }else if(A[i-1] == A[i-2]){
20                 dp = 1;
21             }else{
22                 dp = 2;
23             }
24         }
25         
26         return res;
27     }
28 }

类似Maximum Subarray.

以上是关于LeetCode 978. Longest Turbulent Subarray的主要内容,如果未能解决你的问题,请参考以下文章

978. Longest Turbulent Subarray

978. Longest Turbulent Subarray

leecode 978. Longest Turbulent Subarray(最长连续波动序列,DP or 滚动数组)

LeetCode Longest Palindrome

leetcode:longest words

LeetCode978. 最长湍流子数组