📌  相关文章
📜  以每个数组元素为最大值的最长子数组

📅  最后修改于: 2021-09-06 11:28:21             🧑  作者: Mango

给定一个长度为N的数组arr[] ,任务是为每个数组元素arr[i]找到最长的子数组,其中包含 arr[i] 作为最大值。

例子:

方法:思路是使用Two Pointer技术来解决问题:

  • 初始化两个指针, leftright 。使得对于每个元素arr[i]左侧指向索引[i – 1, 0]以连续方式查找小于或等于 arr[i] 的元素。一旦找到大于arr[i]的元素,指针就会停止。
  • 类似地, right指向索引[i + 1, n – 1]以连续方式查找小于或等于arr[i] 的元素,并在找到任何大于 arr[i] 的元素时停止。
  • 因此, arr[i]最大的最大连续子数组的长度为1 + right – left
  • 对每个数组元素重复上述步骤。

下面是上述方法的实现:

C++
// C++ Program to implement
// the above approach
#include 
using namespace std;
 
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
void solve(int n, int arr[])
{
    int i, ans = 0;
    for (i = 0; i < n; i++) {
 
        // Initialize the bounds
        int left = max(i - 1, 0);
        int right = min(n - 1, i + 1);
 
        // Iterate to find greater
        // element on the left
        while (left >= 0) {
 
            // If greater element is found
            if (arr[left] > arr[i]) {
                left++;
                break;
            }
 
            // Decrement left pointer
            left--;
        }
 
        // If boundary is exceeded
        if (left < 0)
            left++;
 
        // Iterate to find greater
        // element on the right
        while (right < n) {
 
            // If greater element is found
            if (arr[right] > arr[i]) {
                right--;
                break;
            }
            // Increment right pointer
            right++;
        }
 
        // If boundary is exceeded
        if (right >= n)
            right--;
 
        // Length of longest subarray where
        // arr[i] is the largest
        ans = 1 + right - left;
 
        // Print the answer
        cout << ans << " ";
    }
}
 
// Driver Code
int main()
{
    int arr[] = { 4, 2, 1 };
    int n = sizeof arr / sizeof arr[0];
 
    solve(n, arr);
 
    return 0;
}


Java
// Java Program to implement
// the above approach
import java.util.*;
class GFG{
 
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
static void solve(int n, int arr[])
{
    int i, ans = 0;
    for (i = 0; i < n; i++)
    {
 
        // Initialize the bounds
        int left = Math.max(i - 1, 0);
        int right = Math.min(n - 1, i + 1);
 
        // Iterate to find greater
        // element on the left
        while (left >= 0)
        {
 
            // If greater element is found
            if (arr[left] > arr[i])
            {
                left++;
                break;
            }
 
            // Decrement left pointer
            left--;
        }
 
        // If boundary is exceeded
        if (left < 0)
            left++;
 
        // Iterate to find greater
        // element on the right
        while (right < n)
        {
 
            // If greater element is found
            if (arr[right] > arr[i])
            {
                right--;
                break;
            }
           
            // Increment right pointer
            right++;
        }
 
        // If boundary is exceeded
        if (right >= n)
            right--;
 
        // Length of longest subarray where
        // arr[i] is the largest
        ans = 1 + right - left;
 
        // Print the answer
        System.out.print(ans + " ");
    }
}
 
// Driver Code
public static void main(String[] args)
{
    int arr[] = { 4, 2, 1 };
    int n = arr.length;
 
    solve(n, arr);
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program to implement
# the above approach
 
# Function to find the maximum length of
# Subarrays for each element of the array
# having it as the maximum
def solve(n, arr):
     
    ans = 0
     
    for i in range(n):
         
        # Inititalise the bounds
        left = max(i - 1, 0)
        right = min(n - 1, i + 1)
         
        # Iterate to find greater
        # element on the left
        while left >= 0:
             
            # If greater element is found
            if arr[left] > arr[i]:
                left += 1
                break
                 
            # Decrement left pointer
            left -= 1
             
        # If boundary is exceeded
        if left < 0:
            left += 1
             
        # Iterate to find greater
        # element on the right
        while right < n:
             
            # If greater element is found
            if arr[right] > arr[i]:
                right -= 1
                break
             
            # Increment right pointer
            right += 1
         
        # if boundary is exceeded
        if right >= n:
            right -= 1
             
        # Length of longest subarray where
        # arr[i] is the largest
        ans = 1 + right - left
         
        # Print the answer
        print(ans, end = " ")
         
# Driver code
arr = [ 4, 2, 1 ]
n = len(arr)
 
solve(n, arr)
     
# This code is contributed by Stuti Pathak


C#
// C# Program to implement
// the above approach
using System;
class GFG{
 
// Function to find the maximum length of
// Subarrays for each element of the array
// having it as the maximum
static void solve(int n, int []arr)
{
    int i, ans = 0;
    for (i = 0; i < n; i++)
    {
 
        // Initialize the bounds
        int left = Math.Max(i - 1, 0);
        int right = Math.Min(n - 1, i + 1);
 
        // Iterate to find greater
        // element on the left
        while (left >= 0)
        {
 
            // If greater element is found
            if (arr[left] > arr[i])
            {
                left++;
                break;
            }
 
            // Decrement left pointer
            left--;
        }
 
        // If boundary is exceeded
        if (left < 0)
            left++;
 
        // Iterate to find greater
        // element on the right
        while (right < n)
        {
 
            // If greater element is found
            if (arr[right] > arr[i])
            {
                right--;
                break;
            }
           
            // Increment right pointer
            right++;
        }
 
        // If boundary is exceeded
        if (right >= n)
            right--;
 
        // Length of longest subarray where
        // arr[i] is the largest
        ans = 1 + right - left;
 
        // Print the answer
        Console.Write(ans + " ");
    }
}
 
// Driver Code
public static void Main(String[] args)
{
    int []arr = { 4, 2, 1 };
    int n = arr.Length;
 
    solve(n, arr);
}
}
 
// This code is contributed by Princi Singh


Javascript


输出:
3 2 1

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live