最大长度双调子阵列 |设置 2(O(n) 时间和 O(1) 空间)
给定一个包含 n 个正整数的数组 A[0 ... n-1],如果存在 ak 且 i <= k <= j 满足 A[i] = .. A[j,则子数组 A[i ... j] 是双调的– 1] > = A[j]。编写一个函数数组为参数并返回最大长度双调子数组的长度的函数。
我们在下面的帖子中讨论了 O(n) 时间和 O(n) 空间方法。
最大长度双调子阵列 |设置 1(O(n) 时间和 O(n) 空间)
在这组中,我们将讨论占用恒定额外空间的解决方案。
这个想法是检查从 A[i] 开始的最长双调子数组。从 A[i] 开始,我们首先检查上升结束,然后检查下降结束。当沿着当前子数组的斜率下降时,当它找到两个相等的值时,通过记录下一个开始位置来考虑双调子数组的重叠。如果这个子数组的长度大于 max_len,我们将更新 max_len。我们继续这个过程,直到到达数组的末尾。
C++
// C++ program to find length of longest bitonic
// subarray. O(n) time and O(1) extra space
#include
using namespace std;
// Function to find length of longest bitonic
// subarray
int bitonic(int *A, int n)
{
// if A is empty
if (n == 0)
return 0;
// initializing max_len
int maxLen=1;
int start=0;
int nextStart=0;
int j =0;
while (j < n-1)
{
// look for end of ascent
while (j=A[j+1]){
// adjusting nextStart;
// this will be necessarily executed at least once,
// when we detect the start of the descent
if (jA[j+1])
nextStart=j+1;
j++;
}
// updating maxLen, if required
maxLen = max(maxLen,j-(start-1));
start=nextStart;
}
return maxLen;
}
int main()
{
int A[] = {12, 4, 78, 90, 45, 23};
int n = sizeof(A)/sizeof(A[0]);
printf("Length of max length Bitonic "
"Subarray is %d", bitonic(A, n));
return 0;
}
Java
// Java program to find length of longest bitonic
// subarray O(n) time and O(1) extra space
public class MaxLengthBitonic
{
// Method to find length of longest bitonic
// subarray
static int maxLenBitonic(int[] A,int n)
{
// if A is empty
if (n == 0)
return 0;
// initializing max_len
int maxLen=1;
int start=0;
int nextStart=0;
int j =0;
while (j < n-1)
{
// look for end of ascent
while (j=A[j+1]){
// adjusting nextStart;
// this will be necessarily executed at least once,
// when we detect the start of the descent
if (jA[j+1])
nextStart=j+1;
j++;
}
// updating maxLen, if required
maxLen = Math.max(maxLen,j-(start-1));
start=nextStart;
}
return maxLen;
}
public static void main(String[] args)
{
int A[] = {12, 4, 78, 90, 45, 23};
System.out.println("Length of maximal length bitonic " +
"subarray is " + maxLenBitonic(A,A.length));
}
}
// This code is contributed by Markus Schott
Python3
# Python3 program to find length of longest bitonic
# subarray. O(n) time and O(1) extra space
# Function to find length of longest
# bitonic subarray
def bitonic(A, n):
# if A is empty
if (n == 0):
return 0;
# initializing max_len
maxLen = 1;
start = 0;
nextStart = 0;
j = 0;
while (j < n - 1):
# look for end of ascent
while (j < n - 1 and A[j] <= A[j + 1]):
j = j + 1;
# look for end of descent
while (j < n - 1 and A[j] >= A[j + 1]):
# adjusting nextStart;
# this will be necessarily executed
# at least once, when we detect the
# start of the descent
if (j < n - 1 and A[j] > A[j + 1]):
nextStart = j + 1;
j = j + 1;
# updating maxLen, if required
maxLen = max(maxLen, j - (start - 1));
start = nextStart;
return maxLen;
# Driver Code
A = [12, 4, 78, 90, 45, 23];
n = len(A);
print("Length of max length Bitonic Subarray is",
bitonic(A, n));
# This code is contributed by Shivi_Aggarwal
C#
// C# program to find length of longest bitonic
// subarray O(n) time and O(1) extra space
using System;
class MaxLengthBitonic
{
// Method to find length of
// longest bitonic subarray
static int maxLenBitonic(int[] A, int n)
{
// if A is empty
if (n == 0)
return 0;
// initializing max_len
int maxLen = 1;
int start = 0;
int nextStart = 0;
int j = 0;
while (j < n-1)
{
// look for end of ascent
while (j < n-1 && A[j] <= A[j+1])
j++;
// look for end of descent
while (j < n-1 && A[j] >= A[j+1]){
// adjusting nextStart;
// this will be necessarily executed at least once,
// when we detect the start of the descent
if (j < n-1 && A[j] > A[j+1])
nextStart=j + 1;
j++;
}
// updating maxLen, if required
maxLen = Math.Max(maxLen, j - (start - 1));
start=nextStart;
}
return maxLen;
}
public static void Main()
{
int []A = {12, 4, 78, 90, 45, 23};
Console.Write("Length of maximal length bitonic " +
"subarray is " + maxLenBitonic(A, A.Length));
}
}
// This code is contributed by nitin mittal.
PHP
= $A[$j + 1])
{
// adjusting nextStart;
// this will be necessarily
// executed at least once,
// when we detect the start
// of the descent
if ($j < $n - 1 && $A[$j] >
$A[$j + 1])
$nextStart = $j + 1;
$j++;
}
// updating maxLen,
// if required
$maxLen = max($maxLen, $j - ($start - 1));
$start = $nextStart;
}
return $maxLen;
}
// Driver Code
$A = array(12, 4, 78, 90, 45, 23);
$n = sizeof($A);
echo "Length of max length Bitonic "
,"Subarray is ", bitonic($A, $n);
// This code is contributed by nitin mittal.
?>
Javascript
输出:
Length of max length bitonic subarray is 5