给定一个包含N 个元素的数组arr[] ,任务是找出最长的山形子数组。
A mountain sub-array consists of elements that are initially in ascending order until a peak element is reached and beyond the peak element all other elements of the sub-array are in decreasing order.
例子:
Input: arr = [2, 2, 2]
Output: 0
Explanation:
No sub-array exists that shows the behavior of a mountain sub-array.
Input: arr = [1, 3, 1, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5]
Output: 11
Explanation:
There are two sub-arrays that can be considered as mountain sub-arrays. The first one is from index 0 – 2 (3 elements) and next one is from index 2 – 12 (11 elements). As 11 > 2, our answer is 11.
天真的方法:
遍历所有可能的子阵并检查它是否是山子阵。这可能需要很长时间才能找到解决方案,上述方法的时间复杂度可以估计为O(N*N)遍历每个可能的子数组和O(N)以检查它是否是山子数组与否。因此,程序的总时间复杂度为O(N 3 ) ,非常高。
有效的方法:
- 如果给定数组的长度小于 3,则打印 0,因为在这种情况下不可能有山子数组。
- 最初将最大长度设置为 0。
- 使用双指针技术(’begin’ 指针和’end’ 指针)找出给定数组中最长的山子数组。
- 当遇到递增子数组时,在“开始”指针中标记该递增子数组的开始索引。
- 如果在“结束”指针中找到索引值,则重置两个指针中的值,因为它标志着新山子数组的开始。
- 当我们遇到一个递减的子数组时,在’end’指针中标记山子数组的结束索引。
- 计算当前山子阵的长度,与当前遍历的所有山子阵的当前最大长度进行比较,不断更新当前最大长度。
下面是上述高效方法的实现:
C++
// C++ codde for Longest Mountain Subarray
#include
using namespace std;
// Function to find the
// longest mountain subarray
int LongestMountain(vector& a)
{
int i = 0, j = -1,
k = -1, p = 0,
d = 0, n = 0;
// If the size of array is less
// than 3, the array won't show
// mountain like behaviour
if (a.size() < 3) {
return 0;
}
for (i = 0; i < a.size() - 1; i++) {
if (a[i + 1] > a[i]) {
// When a new mountain sub-array
// is found, there is a need to
// set the variables k, j to -1
// in order to help calculate the
// length of new mountain sub-array
if (k != -1) {
k = -1;
j = -1;
}
// j marks the starting index of a
// new mountain sub-array. So set the
// value of j to current index i.
if (j == -1) {
j = i;
}
}
else {
// Checks if next element is
// less than current element
if (a[i + 1] < a[i]) {
// Checks if starting element exists
// or not, if the starting element
// of the mountain sub-array exists
// then the index of ending element
// is stored in k
if (j != -1) {
k = i + 1;
}
// This condition checks if both
// starting index and ending index
// exists or not, if yes, the
// length is calculated.
if (k != -1 && j != -1) {
// d holds the lenght of the
// longest mountain sub-array.
// If the current length is
// greater than the
// calculated length, then
// value of d is updated.
if (d < k - j + 1) {
d = k - j + 1;
}
}
}
// ignore if there is no
// increase or decrease in
// the value of the next element
else {
k = -1;
j = -1;
}
}
}
// Checks and calculates
// the length if last element
// of the array is the last
// element of a mountain sub-array
if (k != -1 && j != -1) {
if (d < k - j + 1) {
d = k - j + 1;
}
}
return d;
}
// Driver code
int main()
{
vector d = { 1, 3, 1, 4,
5, 6, 7, 8,
9, 8, 7, 6, 5 };
cout << LongestMountain(d)
<< endl;
return 0;
}
Java
// Java code for Longest Mountain Subarray
import java.io.*;
class GFG{
// Function to find the
// longest mountain subarray
public static int LongestMountain(int a[])
{
int i = 0, j = -1, k = -1,
p = 0, d = 0, n = 0;
// If the size of array is less than 3,
// the array won't show mountain like
// behaviour
if (a.length < 3)
return 0;
for(i = 0; i < a.length - 1; i++)
{
if (a[i + 1] > a[i])
{
// When a new mountain sub-array is
// found, there is a need to set the
// variables k, j to -1 in order to
// help calculate the length of new
// mountain sub-array
if (k != -1)
{
k = -1;
j = -1;
}
// j marks the starting index of a
// new mountain sub-array. So set the
// value of j to current index i.
if (j == -1)
j = i;
}
else
{
// Checks if next element is
// less than current element
if (a[i + 1] < a[i])
{
// Checks if starting element exists
// or not, if the starting element of
// the mountain sub-array exists then
// the index of ending element is
// stored in k
if (j != -1)
k = i + 1;
// This condition checks if both
// starting index and ending index
// exists or not, if yes,the length
// is calculated.
if (k != -1 && j != -1)
{
// d holds the lenght of the
// longest mountain sub-array.
// If the current length is
// greater than the calculated
// length, then value of d is
// updated.
if (d < k - j + 1)
d = k - j + 1;
}
}
// Ignore if there is no increase
// or decrease in the value of the
// next element
else
{
k = -1;
j = -1;
}
}
}
// Checks and calculates the length
// if last element of the array is
// the last element of a mountain sub-array
if (k != -1 && j != -1)
{
if (d < k - j + 1)
d = k - j + 1;
}
return d;
}
// Driver code
public static void main (String[] args)
{
int a[] = { 1, 3, 1, 4, 5, 6, 7,
8, 9, 8, 7, 6, 5 };
System.out.println(LongestMountain(a));
}
}
// This code is contributed by piyush3010
Python3
# Python3 code for longest mountain subarray
# Function to find the
# longest mountain subarray
def LongestMountain(a):
i = 0
j = -1
k = -1
p = 0
d = 0
n = 0
# If the size of the array is less
# than 3, the array won't show
# mountain like behaviour
if (len(a) < 3):
return 0
for i in range(len(a) - 1):
if (a[i + 1] > a[i]):
# When a new mountain sub-array
# is found, there is a need to
# set the variables k, j to -1
# in order to help calculate the
# length of new mountain sub-array
if (k != -1):
k = -1
j = -1
# j marks the starting index of a
# new mountain sub-array. So set the
# value of j to current index i.
if (j == -1):
j = i
else:
# Checks if next element is
# less than current element
if (a[i + 1] < a[i]):
# Checks if starting element exists
# or not, if the starting element
# of the mountain sub-array exists
# then the index of ending element
# is stored in k
if (j != -1):
k = i + 1
# This condition checks if both
# starting index and ending index
# exists or not, if yes, the
# length is calculated.
if (k != -1 and j != -1):
# d holds the lenght of the
# longest mountain sub-array.
# If the current length is
# greater than the
# calculated length, then
# value of d is updated.
if (d < k - j + 1):
d = k - j + 1
# Ignore if there is no
# increase or decrease in
# the value of the next element
else:
k = -1
j = -1
# Checks and calculates
# the length if last element
# of the array is the last
# element of a mountain sub-array
if (k != -1 and j != -1):
if (d < k - j + 1):
d = k - j + 1
return d
# Driver code
d = [ 1, 3, 1, 4, 5, 6,
7, 8, 9, 8, 7, 6, 5 ]
print(LongestMountain(d))
# This code is contributed by shubhamsingh10
C#
// C# code for the
// longest Mountain Subarray
using System;
class GFG{
// Function to find the
// longest mountain subarray
public static int longestMountain(int []a)
{
int i = 0, j = -1, k = -1,
p = 0, d = 0;
// If the size of array is less than 3,
// the array won't show mountain like
// behaviour
if (a.Length < 3)
return 0;
for(i = 0; i < a.Length - 1; i++)
{
if (a[i + 1] > a[i])
{
// When a new mountain sub-array is
// found, there is a need to set the
// variables k, j to -1 in order to
// help calculate the length of new
// mountain sub-array
if (k != -1)
{
k = -1;
j = -1;
}
// j marks the starting index of a
// new mountain sub-array. So set the
// value of j to current index i.
if (j == -1)
j = i;
}
else
{
// Checks if next element is
// less than current element
if (a[i + 1] < a[i])
{
// Checks if starting element exists
// or not, if the starting element of
// the mountain sub-array exists then
// the index of ending element is
// stored in k
if (j != -1)
k = i + 1;
// This condition checks if both
// starting index and ending index
// exists or not, if yes,the length
// is calculated.
if (k != -1 && j != -1)
{
// d holds the lenght of the
// longest mountain sub-array.
// If the current length is
// greater than the calculated
// length, then value of d is
// updated.
if (d < k - j + 1)
d = k - j + 1;
}
}
// Ignore if there is no increase
// or decrease in the value of the
// next element
else
{
k = -1;
j = -1;
}
}
}
// Checks and calculates the length
// if last element of the array is
// the last element of a mountain sub-array
if (k != -1 && j != -1)
{
if (d < k - j + 1)
d = k - j + 1;
}
return d;
}
// Driver code
public static void Main(String[] args)
{
int []a = {1, 3, 1, 4, 5, 6, 7,
8, 9, 8, 7, 6, 5};
Console.WriteLine(longestMountain(a));
}
}
// This code is contributed by Princi Singh
Javascript
11
时间复杂度: O(N)
辅助空间复杂度: O(1)
如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live