📌  相关文章
📜  除去M个项目后最少数量的不同元素|套装2

📅  最后修改于: 2021-05-14 01:06:32             🧑  作者: Mango

给定项目数组,第ith个索引元素表示项目ID,给定数字m,则任务是删除m个元素,以使剩余的唯一ID最少。打印不同ID的数量。

例子:

对于O(N * log N)方法,请参阅上一篇文章。

高效的方法:想法是将每个元素的出现存储在哈希中,并再次计算每个频率在哈希中的出现。步骤如下:

  1. 遍历给定的数组元素并计算每个数字的出现次数并将其存储到哈希中。
  2. 现在,无需对频率进行排序,而是将频率的出现计数到另一个数组中,例如fre _ arr []
  3. 计算总的唯一编号(例如ans )作为不同ID的数量。
  4. 现在,遍历freq_arr []数组,如果freq_arr [i]> 0,则从ans中删除最小值m / ifreq_arr [i] (例如t ),并从m中减去i * t以删除出现的任何数字i来自m

下面是上述方法的实现。

C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function to return minimum distinct
// character after M removals
int distinctNumbers(int arr[], int m,
                    int n)
{
    unordered_map count;
 
    // Count the occurences of number
    // and store in count
    for (int i = 0; i < n; i++)
        count[arr[i]]++;
 
    // Count the occurences of the
    // frequencies
    vector fre_arr(n + 1, 0);
    for (auto it : count) {
        fre_arr[it.second]++;
    }
 
    // Take answer as total unique numbers
    // and remove the frequency and
    // subtract the answer
    int ans = count.size();
 
    for (int i = 1; i <= n; i++) {
        int temp = fre_arr[i];
        if (temp == 0)
            continue;
 
        // Remove the minimum number
        // of times
        int t = min(temp, m / i);
        ans -= t;
        m -= i * t;
    }
 
    // Return the answer
    return ans;
}
 
// Driver Code
int main()
{
    // Initialize array
    int arr[] = { 2, 4, 1, 5, 3, 5, 1, 3 };
 
    // Size of array
    int n = sizeof(arr) / sizeof(arr[0]);
    int m = 2;
 
    // Function call
    cout << distinctNumbers(arr, m, n);
    return 0;
}


Java
// Java program to implement the
// above approach
import java.util.*;
 
class GFG{
 
// Function to return minimum distinct
// character after M removals
static int distinctNumbers(int arr[], int m,
                                      int n)
{
    Map count = new HashMap();
 
    // Count the occurences of number
    // and store in count
    for (int i = 0; i < n; i++)
    count.put(arr[i],
              count.getOrDefault(arr[i], 0) + 1);
     
    // Count the occurences of the
    // frequencies
    int[] fre_arr = new int[n + 1];
    for(Integer it : count.values())
    {
        fre_arr[it]++;
    }
 
    // Take answer as total unique numbers
    // and remove the frequency and
    // subtract the answer
    int ans = count.size();
 
    for(int i = 1; i <= n; i++)
    {
        int temp = fre_arr[i];
        if (temp == 0)
            continue;
 
        // Remove the minimum number
        // of times
        int t = Math.min(temp, m / i);
        ans -= t;
        m -= i * t;
    }
 
    // Return the answer
    return ans;
}
 
// Driver code
public static void main(String[] args)
{
     
    // Initialize array
    int arr[] = { 2, 4, 1, 5, 3, 5, 1, 3 };
     
    // Size of array
    int n = arr.length;
    int m = 2;
     
    // Function call
    System.out.println(distinctNumbers(arr, m, n));
}
}
 
// This code is contributed by offbeat


Python3
# Python3 program for the above approach
 
# Function to return minimum distinct
# character after M removals
def distinctNumbers(arr, m, n):
 
    count = {}
 
    # Count the occurences of number
    # and store in count
    for i in range(n):
        count[arr[i]] = count.get(arr[i], 0) + 1
 
    # Count the occurences of the
    # frequencies
    fre_arr = [0] * (n + 1)
    for it in count:
        fre_arr[count[it]] += 1
 
    # Take answer as total unique numbers
    # and remove the frequency and
    # subtract the answer
    ans = len(count)
 
    for i in range(1, n + 1):
        temp = fre_arr[i]
        if (temp == 0):
            continue
             
        # Remove the minimum number
        # of times
        t = min(temp, m // i)
        ans -= t
        m -= i * t
 
    # Return the answer
    return ans
 
# Driver Code
if __name__ == '__main__':
 
    # Initialize array
    arr = [ 2, 4, 1, 5, 3, 5, 1, 3 ]
 
    # Size of array
    n = len(arr)
    m = 2
 
    # Function call
    print(distinctNumbers(arr, m, n))
 
# This code is contributed by mohit kumar 29


C#
// C# program to implement the
// above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function to return minimum distinct
// character after M removals
static int distinctNumbers(int []arr,
                           int m, int n)
{
  Dictionary count = new Dictionary();
   
  // Count the occurences of number
  // and store in count
  for (int i = 0; i < n; i++)
    if(count.ContainsKey(arr[i]))
    {
      count[arr[i]] = count[arr[i]] + 1;
    }
  else
  {
    count.Add(arr[i], 1);
  }
 
  // Count the occurences of the
  // frequencies
  int[] fre_arr = new int[n + 1];
  foreach(int it in count.Values)
  {
    fre_arr[it]++;
  }
 
  // Take answer as total unique numbers
  // and remove the frequency and
  // subtract the answer
  int ans = count.Count;
 
  for(int i = 1; i <= n; i++)
  {
    int temp = fre_arr[i];
    if (temp == 0)
      continue;
 
    // Remove the minimum number
    // of times
    int t = Math.Min(temp, m / i);
    ans -= t;
    m -= i * t;
  }
 
  // Return the answer
  return ans;
}
 
// Driver code
public static void Main(String[] args)
{
  // Initialize array
  int []arr = {2, 4, 1, 5, 3, 5, 1, 3};
 
  // Size of array
  int n = arr.Length;
  int m = 2;
 
  // Function call
  Console.WriteLine(distinctNumbers(arr, m, n));
}
}
 
// This code is contributed by Princi Singh


输出:
3


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