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. 

Note: corner case {1, 1, 2}. The final res needs to be updated again. Otherwise, the result is 1 not 2.

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                 res = Math.max(res, dp[i]);
26             }
27         }
28 
29         return res;
30     }
31 }

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

类似Maximum Subarray.

posted @ 2019-09-08 00:57  Dylan_Java_NYC  阅读(513)  评论(0编辑  收藏  举报