📌  相关文章
📜  需要最少的删除,以便任何数字 X 将恰好出现 X 次

📅  最后修改于: 2021-10-27 03:19:50             🧑  作者: Mango

给定的阵列ARR [] N个整数的,任务是找到最小缺失阵列对于i的所有可能值中需要使得ARR的频率[i]的准确的常用3 [i]中
例子:

处理方法:有两种情况:

  • 如果 X 的频率大于或等于 o X,那么我们删除 X 的额外频率以获得值 X 的 X 个元素。
  • 如果 X 的频率小于 X,那么我们删除所有出现的 X,因为不可能获得值 X 的额外元素。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
 
// Function to return the minimum
// deletions required
int MinDeletion(int a[], int n)
{
 
    // To store the frequency of
    // the array elements
    unordered_map map;
 
    // Store frequency of each element
    for (int i = 0; i < n; i++)
        map[a[i]]++;
 
    // To store the minimum deletions required
    int ans = 0;
 
    for (auto i : map) {
 
        // Value
        int x = i.first;
 
        // It's frequency
        int frequency = i.second;
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency) {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    }
 
    return ans;
}
 
// Driver code
int main()
{
    int a[] = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
    int n = sizeof(a) / sizeof(a[0]);
 
    cout << MinDeletion(a, n);
 
    return 0;
}


Java
// Java Implementation of above approach
import java.util.*;
 
class GFG
{
     
// Function to return the minimum
// deletions required
static int MinDeletion(int a[], int n)
{
 
    // To store the frequency of
    // the array elements
    Map mp = new HashMap<>();
 
    // Store frequency of each element
    for (int i = 0 ; i < n; i++)
    {
        if(mp.containsKey(a[i]))
        {
            mp.put(a[i], mp.get(a[i])+1);
        }
        else
        {
            mp.put(a[i], 1);
        }
    }
    // To store the minimum deletions required
    int ans = 0;
 
    for (Map.Entry i : mp.entrySet())
    {
 
        // Value
        int x = i.getKey();
 
        // It's frequency
        int frequency = i.getValue();
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency)
        {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    }
 
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
    int a[] = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
    int n = a.length;
 
    System.out.println(MinDeletion(a, n));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 implementation of the approach
 
# Function to return the minimum
# deletions required
def MinDeletion(a, n) :
 
    # To store the frequency of
    # the array elements
    map = dict.fromkeys(a, 0);
 
    # Store frequency of each element
    for i in range(n) :
        map[a[i]] += 1;
 
    # To store the minimum deletions required
    ans = 0;
 
    for key,value in map.items() :
 
        # Value
        x = key;
 
        # It's frequency
        frequency = value;
 
        # If number less than or equal
        # to it's frequency
        if (x <= frequency) :
 
            # Delete extra occurrences
            ans += (frequency - x);
 
        # Delete every occurrence of x
        else :
            ans += frequency;
 
    return ans;
 
 
# Driver code
if __name__ == "__main__" :
 
    a = [ 2, 3, 2, 3, 4, 4, 4, 4, 5 ];
    n = len(a);
 
    print(MinDeletion(a, n));
 
# This code is contributed by AnkitRai01


C#
// C# Implementation of above approach
using System;
using System.Collections.Generic;
 
class GFG
{
     
// Function to return the minimum
// deletions required
static int MinDeletion(int []a, int n)
{
 
    // To store the frequency of
    // the array elements
    Dictionary mp = new Dictionary();
 
    // Store frequency of each element
    for (int i = 0 ; i < n; i++)
    {
        if(mp.ContainsKey(a[i]))
        {
            var val = mp[a[i]];
            mp.Remove(a[i]);
            mp.Add(a[i], val + 1);
        }
        else
        {
            mp.Add(a[i], 1);
        }
    }
     
    // To store the minimum deletions required
    int ans = 0;
 
    foreach(KeyValuePair i in mp)
    {
 
        // Value
        int x = i.Key;
 
        // It's frequency
        int frequency = i.Value;
 
        // If number less than or equal
        // to it's frequency
        if (x <= frequency)
        {
 
            // Delete extra occurrences
            ans += (frequency - x);
        }
 
        // Delete every occurrence of x
        else
            ans += frequency;
    }
 
    return ans;
}
 
// Driver code
public static void Main(String[] args)
{
    int []a = { 2, 3, 2, 3, 4, 4, 4, 4, 5 };
    int n = a.Length;
 
    Console.WriteLine(MinDeletion(a, n));
}
}
 
// This code is contributed by PrinciRaj1992


Javascript


输出:
3

时间复杂度: O(N)

如果您希望与专家一起参加现场课程,请参阅DSA 现场工作专业课程学生竞争性编程现场课程