📌  相关文章
📜  通过在数组的每次迭代中重复删除它们来最小化数组中的峰值元素

📅  最后修改于: 2021-04-24 23:55:10             🧑  作者: Mango

给定由N个不同的正整数组成的数组arr [] ,任务是从给定数组中重复查找最小峰元素,然后删除该元素,直到删除所有数组元素。

例子:

方法:这个想法是通过使用两个嵌套循环在数组上进行迭代来找到数组的最小峰值元素,其中,外循环指向当前元素,而内循环执行以找到最小峰值元素的索引,删除该峰值数组中的元素并将当前峰值元素存储在结果列表中。完成上述步骤后,打印列表中存储的所有最小峰元素。

下面是上述方法的实现:

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 elemnt 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 elemnt 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 elemnt 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 elemnt 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


输出:
[6, 8, 9, 7, 2, 1]







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