📌  相关文章
📜  最长的山子阵列

📅  最后修改于: 2021-05-14 01:36:20             🧑  作者: Mango

给定具有N个元素的数组arr [] ,任务是找出最长的子数组,其形状为山形。

例子:

天真的方法:
遍历所有可能的子阵列,然后检查它是否是山脉子阵列。可能需要很长时间才能找到解决方案,并且上述方法的时间复杂度可以估算为O(N * N)遍历每个可能的子数组,而O(N)则检查它是否是山峰子方程。数组与否。因此,该程序的总时间复杂度为O(N 3 ) ,非常高。

高效方法:

  1. 如果给定数组的长度小于3,则打印0,因为在这种情况下不可能有山峰子数组。
  2. 最初将最大长度设置为0。
  3. 使用两指针技术(“ begin”指针和“ end”指针)找出给定数组中最长的Mountain子数组。
  4. 当遇到递增子数组时,请在“开始”指针中标记该递增子数组的开始索引。
  5. 如果在“ end”指针中找到索引值,则重置两个指针中的值,因为它标志着新的山峰子数组的开始。
  6. 当遇到一个递减的子数组时,在“ end”指针中标记山子数组的结束索引。
  7. 计算当前的山子阵列的长度,将其与到目前为止遍历的所有山峰子阵列的当前最大长度进行比较,并继续更新当前的最大长度。

以下是上述有效方法的实现:

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


输出:
11



时间复杂度: O(N)
辅助空间复杂度: O(1)