📜  删除 M 项后的最小不同元素数 | 2套

📅  最后修改于: 2021-09-07 02:16:53             🧑  作者: Mango

给定一个项目数组,第 i 个索引元素表示项目 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从

下面是上述方法的实现。

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


Javascript


输出:
3

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

如果您想与行业专家一起参加直播课程,请参阅Geeks Classes Live