📌  相关文章
📜  给定数组中峰值元素之间的最小距离

📅  最后修改于: 2022-05-13 01:56:09.534000             🧑  作者: Mango

给定数组中峰值元素之间的最小距离

给定一个数组arr[] ,任务是找到给定数组中两个峰值元素之间的最小距离。

例子:

方法:给定的问题可以通过观察这样一个事实来解决,即为了使距离最小,只需要考虑相邻峰值元素的距离。因此,迭代给定的数组并为每个峰值元素计算它与最后找到的峰值元素的距离,该元素的索引可以在变量idx中维护。所有这些距离中的最小值就是所需的答案。

下面是上述方法的实现:

C++
// C++ implementation of the above approach
#include 
using namespace std;
 
// Function to find the minimum distance
// between two peak elements
void MinimumDistance(int arr[], int n)
{
   
    // Stores the minimum distance between
    // two peak elements
    int mn = INT_MAX;
 
    // Store the index of peak element
    int idx = -1;
 
    // Checking for the 1st elements of array
    if (arr[0] >= arr[1]) {
        idx = 0;
    }
    for (int i = 1; i < n - 1; i++) {
        if (arr[i] >= arr[i - 1]
            && arr[i] >= arr[i + 1]) {
            if (idx == -1) {
                idx = i;
            }
            else {
                mn = min(mn, i - idx);
            }
            idx = i;
        }
    }
 
    // Checking for last element of the array
    if (arr[n - 1] >= arr[n - 2] && idx != -1) {
        mn = min(mn, n - 1 - idx);
    }
 
    // If number of peak elements is less than 2
    if (mn == INT_MAX)
        cout << -1;
 
    // Print Answer
    else
        cout << mn;
}
 
// Driver Code
int main()
{
    int arr[] = { 2, 3, 1, 2, 4, 1, 2 };
    int n = sizeof(arr) / sizeof(arr[0]);
     
    MinimumDistance(arr, n);
     
    return 0;
}
 
// This code is contributed by Samim Hossain Mondal.


Java
// Java implementation of the above approach
import java.io.*;
import java.util.*;
class GFG {
 
    // Function to find the minimum distance
    // between two peak elements
    public static void MinimumDistance(int[] arr)
    {
        // Stores the minimum distance between
        // two peak elements
        int min = Integer.MAX_VALUE;
 
        // Store the index of peak element
        int idx = -1;
        int n = arr.length;
 
        // Checking for the 1st elements of array
        if (arr[0] >= arr[1]) {
            idx = 0;
        }
        for (int i = 1; i < n - 1; i++) {
            if (arr[i] >= arr[i - 1]
                && arr[i] >= arr[i + 1]) {
                if (idx == -1) {
                    idx = i;
                }
                else {
                    min = Math.min(min, i - idx);
                }
                idx = i;
            }
        }
 
        // Checking for last element of the array
        if (arr[n - 1] >= arr[n - 2] && idx != -1) {
            min = Math.min(min, n - 1 - idx);
        }
 
        // If number of peak elements is less than 2
        if (min == Integer.MAX_VALUE)
            System.out.println(-1);
 
        // Print Answer
        else
            System.out.println(min);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 2, 3, 1, 2, 4, 1, 2 };
        MinimumDistance(arr);
    }
}


Python3
# Python implementation of the above approach
 
# Function to find the minimum distance
# between two peak elements
def MinimumDistance(arr):
   
  # Stores the minimum distance between
  # two peak elements
  less = 10**9;
 
  # Store the index of peak element
  idx = -1;
  n = len(arr);
 
  # Checking for the 1st elements of array
  if (arr[0] >= arr[1]):
    idx = 0;
 
  for i in range(1, n - 1):
    if (arr[i] >= arr[i - 1] and arr[i] >= arr[i + 1]):
      if (idx == -1):
        idx = i;
      else:
        less = min(less, i - idx);
      idx = i;
     
 
  # Checking for last element of the array
  if (arr[n - 1] >= arr[n - 2] and idx != -1):
    less = min(less, n - 1 - idx);
 
 
  # If number of peak elements is less than 2
  if (less == 10**9):
    print(-1);
 
  # Print Answer
  else:
    print(less);
 
# Driver Code
 
arr = [2, 3, 1, 2, 4, 1, 2];
MinimumDistance(arr);
 
# This code is contributed by gfgking


C#
// C# implementation of the above approach
using System;
class GFG
{
 
    // Function to find the minimum distance
    // between two peak elements
    public static void MinimumDistance(int[] arr)
    {
       
        // Stores the minimum distance between
        // two peak elements
        int min = int.MaxValue;
 
        // Store the index of peak element
        int idx = -1;
        int n = arr.Length;
 
        // Checking for the 1st elements of array
        if (arr[0] >= arr[1])
        {
            idx = 0;
        }
        for (int i = 1; i < n - 1; i++)
        {
            if (arr[i] >= arr[i - 1]
                && arr[i] >= arr[i + 1])
            {
                if (idx == -1)
                {
                    idx = i;
                }
                else
                {
                    min = Math.Min(min, i - idx);
                }
                idx = i;
            }
        }
 
        // Checking for last element of the array
        if (arr[n - 1] >= arr[n - 2] && idx != -1)
        {
            min = Math.Min(min, n - 1 - idx);
        }
 
        // If number of peak elements is less than 2
        if (min == int.MaxValue)
            Console.Write(-1);
 
        // Print Answer
        else
            Console.Write(min);
    }
 
    // Driver Code
    public static void Main()
    {
        int[] arr = { 2, 3, 1, 2, 4, 1, 2 };
        MinimumDistance(arr);
    }
}
 
// This code is contributed by gfgking


Javascript


输出
2

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