给定一个由N 个不同的正整数组成的数组arr[] ,任务是从给定的数组中重复找到最小峰值元素并删除该元素,直到删除所有数组元素。
Peak Element: Any element in the array is know as the peak element based on the following conditions:
- If arr[i – 1] < arr[i] > arr[i + 1], where 1 < i < N – 1, then arr[i] is the peak element.
- If arr[0] > arr[1], then arr[0] is the peak element, where N is the size of the array.
- If arr[N – 2] < arr[N – 1], then arr[N – 1] is the peak element, where N is the size of the array.
If more than one peak element exists in the array, then the minimum value among them needs to be printed.
例子:
Input: arr[] = {1, 9, 7, 8, 2, 6}
Output: [6, 8, 9, 7, 2, 1]
Explanation:
First min peak = 6, as 2 < 6.
The array after removing min peak will be [1, 9, 7, 8, 2].
Second min peak = 8, as 7 < 8 > 2.
The array after removing min peak will be [1, 9, 7, 2]
Third min peak = 9, as 1 < 9 > 7.
The array after removing min peak will be [1, 7, 2]
Fourth min peak = 7, as 1 < 7 > 2.
The array after removing min peak will be [1, 2]
Fifth min peak = 2, as 1 < 2.
The array after removing min peak will be [1]
Sixth min peak = 1.
Therefore, the list of minimum peak is [6, 8, 9, 7, 2, 1].
Input: arr []= {1, 5, 3, 7, 2}
Output: [5, 7, 3, 2, 1]
Explanation:
First min peak = 5, as 1 < 5 > 3.
The array after removing min peak will be [1, 3, 7, 2]
Second min peak = 7, as 3 < 7 > 2.
The array after removing min peak will be [1, 3, 2]
Third min peak = 3, as 1 < 3 > 2.
The array after removing min peak will be [1, 2]
Fourth min peak = 2, as 1 < 2.
The array after removing min peak will be [1]
Fifth min peak = 1.
Therefore, the list of minimum peak is [5, 7, 3, 2, 1]
方法:想法是通过使用两个嵌套循环遍历数组来找到数组的最小峰值元素,其中外循环指向当前元素,内循环执行以找到最小峰值元素的索引,删除该峰值数组中的元素并将当前峰值元素存储在结果列表中。完成上述步骤后,打印所有存储在列表中的最小峰元素。
下面是上述方法的实现:
C++
// C++ program for the above approach
#include
using namespace std;
// Function to return the list of
// minimum peak elements
void minPeaks(vectorlist)
{
// Length of original list
int n = list.size();
// Initialize resultant list
vectorresult;
// Traverse each element of list
for(int i = 0; i < n; i++)
{
int min = INT_MAX;
int index = -1;
// Length of original list after
// removing the peak element
int size = list.size();
// Traverse new list after removal
// of previous min peak element
for(int j = 0; j < size; j++)
{
// Update min and index,
// if first element of
// list > next element
if (j == 0 && j + 1 < size)
{
if (list[j] > list[j + 1] &&
min > list[j])
{
min = list[j];
index = j;
}
}
else if (j == size - 1 &&
j - 1 >= 0)
{
// Update min and index,
// if last element of
// list > previous one
if (list[j] > list[j - 1] &&
min > list[j])
{
min = list[j];
index = j;
}
}
// Update min and index, if
// list has single element
else if (size == 1)
{
min = list[j];
index = j;
}
// Update min and index,
// if current element >
// adjacent elements
else if (list[j] > list[j - 1] &&
list[j] > list[j + 1] &&
min > list[j])
{
min = list[j];
index = j;
}
}
// Remove current min peak
// element from list
list.erase(list.begin() + index);
// Insert min peak into
// resultant list
result.push_back(min);
}
// Print resultant list
cout << "[";
for(int i = 0; i < result.size(); i++)
{
cout << result[i] << ", ";
}
cout << "]";
}
// Driver Code
int main()
{
// Given array arr[]
vector arr = { 1, 9, 7, 8, 2, 6 };
// Function call
minPeaks(arr);
}
// This code is contributed by bikram2001jha
Java
// Java program for the above approach
import java.util.*;
import java.lang.*;
class GFG {
// Function to return the list of
// minimum peak elements
static void
minPeaks(ArrayList list)
{
// Length of original list
int n = list.size();
// Initialize resultant list
ArrayList result
= new ArrayList<>();
// Traverse each element of list
for (int i = 0; i < n; i++) {
int min = Integer.MAX_VALUE;
int index = -1;
// Length of original list after
// removing the peak element
int size = list.size();
// Traverse new list after removal
// of previous min peak element
for (int j = 0; j < size; j++) {
// Update min and index,
// if first element of
// list > next element
if (j == 0 && j + 1 < size) {
if (list.get(j) > list.get(j + 1)
&& min > list.get(j)) {
min = list.get(j);
index = j;
}
}
else if (j == size - 1
&& j - 1 >= 0) {
// Update min and index,
// if last element of
// list > previous one
if (list.get(j)
> list.get(j - 1)
&& min
> list.get(j)) {
min = list.get(j);
index = j;
}
}
// Update min and index, if
// list has single element
else if (size == 1) {
min = list.get(j);
index = j;
}
// Update min and index,
// if current element >
// adjacent elements
else if (list.get(j)
> list.get(j - 1)
&& list.get(j)
> list.get(j + 1)
&& min
> list.get(j)) {
min = list.get(j);
index = j;
}
}
// Remove current min peak
// element from list
list.remove(index);
// Insert min peak into
// resultant list
result.add(min);
}
// Print resultant list
System.out.println(result);
}
// Driver Code
public static void main(String[] args)
{
// Given array arr[]
ArrayList arr = new ArrayList<>(
Arrays.asList(1, 9, 7, 8, 2, 6));
// Function Call
minPeaks(arr);
}
}
Python3
# Python3 program for
# the above approach
import sys
# Function to return the list of
# minimum peak elements
def minPeaks(list1):
# Length of original list
n = len(list1)
# Initialize resultant list
result = []
# Traverse each element of list
for i in range (n):
min = sys.maxsize
index = -1
# Length of original list
# after removing the peak
# element
size = len(list1)
# Traverse new list after removal
# of previous min peak element
for j in range (size):
# Update min and index,
# if first element of
# list > next element
if (j == 0 and j + 1 < size):
if (list1[j] > list1[j + 1] and
min > list1[j]):
min = list1[j];
index = j;
elif (j == size - 1 and
j - 1 >= 0):
# Update min and index,
# if last element of
# list > previous one
if (list1[j] > list1[j - 1] and
min > list1[j]):
min = list1[j]
index = j
# Update min and index, if
# list has single element
elif (size == 1):
min = list1[j]
index = j
# Update min and index,
# if current element >
# adjacent elements
elif (list1[j] > list1[j - 1] and
list1[j] > list1[j + 1] and
min > list1[j]):
min = list1[j]
index = j
# Remove current min peak
# element from list
list1.pop(index)
# Insert min peak into
# resultant list
result.append(min)
# Print resultant list
print (result)
# Driver Code
if __name__ == "__main__":
# Given array arr[]
arr = [1, 9, 7, 8, 2, 6]
# Function call
minPeaks(arr)
# This code is contributed by Chitranayal
C#
// C# program for
// the above approach
using System;
using System.Collections.Generic;
class GFG{
// Function to return the list of
// minimum peak elements
static void minPeaks(List list)
{
// Length of original list
int n = list.Count;
// Initialize resultant list
List result = new List();
// Traverse each element of list
for (int i = 0; i < n; i++)
{
int min = int.MaxValue;
int index = -1;
// Length of original list after
// removing the peak element
int size = list.Count;
// Traverse new list after removal
// of previous min peak element
for (int j = 0; j < size; j++)
{
// Update min and index,
// if first element of
// list > next element
if (j == 0 && j + 1 < size)
{
if (list[j] > list[j + 1] &&
min > list[j])
{
min = list[j];
index = j;
}
}
else if (j == size - 1 && j - 1 >= 0)
{
// Update min and index,
// if last element of
// list > previous one
if (list[j] > list[j - 1] &&
min > list[j])
{
min = list[j];
index = j;
}
}
// Update min and index, if
// list has single element
else if (size == 1)
{
min = list[j];
index = j;
}
// Update min and index,
// if current element >
// adjacent elements
else if (list[j] > list[j - 1] &&
list[j] > list[j + 1] &&
min > list[j])
{
min = list[j];
index = j;
}
}
// Remove current min peak
// element from list
list.RemoveAt(index);
// Insert min peak into
// resultant list
result.Add(min);
}
// Print resultant list
for (int i = 0; i < result.Count; i++)
Console.Write(result[i] +", ");
}
// Driver Code
public static void Main(String[] args)
{
// Given array []arr
List arr = new List{1, 9, 7,
8, 2, 6};
// Function Call
minPeaks(arr);
}
}
// This code is contributed by 29AjayKumar
Javascript
[6, 8, 9, 7, 2, 1]
时间复杂度: O(N 2 )
辅助空间: O(N)
如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程和学生竞争性编程现场课程。