当前位置 : 主页 > 网页制作 > HTTP/TCP >

LeetCode 978. Longest Turbulent Subarray

来源:互联网 收集:自由互联 发布时间:2021-06-16
原题链接在这里: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 j , A[k] A[k+1] when k is odd, and A[k] A[k+1] when k is e

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

网友评论