📌  相关文章
📜  清空数组所需的K个相等元素的最小移除

📅  最后修改于: 2021-05-17 19:37:33             🧑  作者: Mango

给定一个由N个整数组成的数组arr [] ,任务是计算最多需要删除K个相等元素以使该数组为空的最小次数。

例子:

天真的方法:最简单的方法是遍历数组并计算每个数组元素的频率,然后将每个元素的频率除以K并将其加到count 。如果数组元素的频率不能被K整除,则增加计数。完成上述步骤后,打印count的值作为结果。
时间复杂度: O(N 2 )
辅助空间: O(1)

高效方法:可以通过哈希优化上述方法,以存储每个数组元素的频率,然后计算所需的最少操作数。请按照以下步骤解决问题:

  • 初始化一个变量,例如count ,该变量存储所需的最少步骤数。
  • 初始化一个哈希表,该哈希表存储数组中每个元素的频率。
  • 遍历数组arr []并将每个元素的频率存储在Hashmap中。
  • 遍历Hashmap,然后将每个元素的频率值(除以K )加到变量count上。如果当前数组元素的频率不能被K整除,则将count递增1
  • 完成上述步骤后,打印计数为使阵列为空所需的最小步骤数。

下面是上述方法的实现:

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to count the minimum
// number of steps required to empty
// given array by removing at most K
// equal array elements in each operation
void minSteps(int arr[], int N, int K)
{
   
    // Stores the minimum number of
    // steps required to empty the array
    int count = 0;
 
    // Stores the occurrence
    // of each array element
    map cntFreq;
    for (int i = 0; i < N; i++)
    {
 
        // Update the frequency
        cntFreq[arr[i]]++;
    }
 
    // Traverse the Hashmap
    for (auto i : cntFreq)
    {
 
        // Check if the frequency
        // is divisible by K or not
        if (i.first % K == 0)
            count += i.second / K;
 
        // Otherwise
        else
            count += (i.second / K) + 1;
    }
 
    // Print the count of
    // minimum steps required
    cout << (count);
}
 
// Driver Code
int main()
{
 
    int arr[] = { 4, 4, 7, 3, 1,
                 1, 2, 1, 7, 3 };
    int N = sizeof(arr) / sizeof(arr[0]);
    int K = 5;
    minSteps(arr, N, K);
    return 0;
}
 
// This code is contributed by Dharanendra L V.


Java
// Java program for the above approach
 
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
 
class GFG {
 
    // Function to count the minimum
    // number of steps required to empty
    // given array by removing at most K
    // equal array elements in each operation
    public static void minSteps(
        int[] arr, int N, int K)
    {
        // Stores the minimum number of
        // steps required to empty the array
        int count = 0;
 
        // Stores the occurrence
        // of each array element
        Map cntFreq
            = new HashMap();
 
        for (int i = 0; i < N; i++) {
 
            // Update the frequency
            cntFreq.put(
                arr[i],
                cntFreq.getOrDefault(
                    arr[i], 0)
                    + 1);
        }
 
        // Traverse the Hashmap
        for (Integer i : cntFreq.keySet()) {
 
            // Check if the frequency
            // is divisible by K or not
            if (cntFreq.get(i) % K == 0)
                count += cntFreq.get(i)
                         / K;
 
            // Otherwise
            else
                count += (cntFreq.get(i)
                          / K)
                         + 1;
        }
 
        // Print the count of
        // minimum steps required
        System.out.print(count);
    }
 
    // Driver Code
    public static void main(String[] args)
    {
        int arr[] = { 4, 4, 7, 3, 1,
                      1, 2, 1, 7, 3 };
        int N = arr.length;
        int K = 5;
 
        minSteps(arr, N, K);
    }
}


Python3
# Python3 program for the above approach
 
# Function to count the minimum
# number of steps required to empty
# given array by removing at most K
# equal array elements in each operation
def minSteps(arr, N, K) :
    
    # Stores the minimum number of
    # steps required to empty the array
    count = 0
  
    # Stores the occurrence
    # of each array element
    cntFreq = {}
    for i in range(N) :
  
        # Update the frequency
        if arr[i] in cntFreq :
            cntFreq[arr[i]] += 1
        else :
            cntFreq[arr[i]] = 1
  
    # Traverse the Hashmap
    for i in cntFreq :
  
        # Check if the frequency
        # is divisible by K or not
        if (i % K == 0) :
            count += cntFreq[i] // K
  
        # Otherwise
        else :
            count += (cntFreq[i] // K) + 1
  
    # Print the count of
    # minimum steps required
    print(count)
     
arr = [ 4, 4, 7, 3, 1, 1, 2, 1, 7, 3 ]
N = len(arr)
K = 5
minSteps(arr, N, K)
 
# This code is contributed by divyeshabadiya07.


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
public class GFG
{
 
    // Function to count the minimum
    // number of steps required to empty
    // given array by removing at most K
    // equal array elements in each operation
    public static void minSteps(
        int[] arr, int N, int K)
    {
        // Stores the minimum number of
        // steps required to empty the array
        int count = 0;
 
        // Stores the occurrence
        // of each array element
        Dictionary cntFreq
            = new Dictionary();
 
        for (int i = 0; i < N; i++) {
 
            // Update the frequency
            if(cntFreq.ContainsKey(arr[i]))
                cntFreq[arr[i]] = cntFreq[arr[i]]+1;
            else
                 
            cntFreq.Add(arr[i],1);
        }
 
        // Traverse the Hashmap
        foreach (int i in cntFreq.Keys) {
 
            // Check if the frequency
            // is divisible by K or not
            if (cntFreq[i] % K == 0)
                count += cntFreq[i]
                         / K;
 
            // Otherwise
            else
                count += (cntFreq[i]
                          / K)
                         + 1;
        }
 
        // Print the count of
        // minimum steps required
        Console.Write(count);
    }
 
    // Driver Code
    public static void Main(String[] args)
    {
        int []arr = { 4, 4, 7, 3, 1,
                      1, 2, 1, 7, 3 };
        int N = arr.Length;
        int K = 5;
 
        minSteps(arr, N, K);
    }
}
 
// This code is contributed by shikhasingrajput


输出:
5

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