博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LeetCode 978. Longest Turbulent Subarray
阅读量:4621 次
发布时间:2019-06-09

本文共 2684 字,大约阅读时间需要 8 分钟。

原题链接在这里:

题目:

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: 5Explanation: (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]){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]){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 }

类似.

转载于:https://www.cnblogs.com/Dylan-Java-NYC/p/11484108.html

你可能感兴趣的文章
每日站立会议个人博客五
查看>>
ddd
查看>>
死磕 java同步系列之AQS起篇
查看>>
利用Lucene把文本的字体格式进行改动,然后输出到一个新的文件里
查看>>
[Openstack] Expecting an auth URL via either --os-auth-url or env[OS_AUTH_URL]
查看>>
How to Create Modifiers Using the API QP_MODIFIERS_PUB.PROCESS_MODIFIERS
查看>>
待飞笔记(第一天 )
查看>>
解惑好文:移动端H5页面高清多屏适配方案
查看>>
traefik添加多证书
查看>>
PhantomJs 笔记
查看>>
js设计模式--语言类型
查看>>
C#多线程之二:ManualResetEvent和AutoResetEvent
查看>>
忽略UserInterfaceState.xcuserstate
查看>>
ReactNative--Flexbox布局
查看>>
java实现读取文件大全
查看>>
[Cordova] 无法显示Alert视窗
查看>>
借助过度区选择阈值
查看>>
评论列表显示及排序,个人中心显示
查看>>
JavaWeb学习笔记总结 目录篇
查看>>
C#根据html生成PDF
查看>>