📌  相关文章
📜  删除 m 个项目后不同元素的最小数量

📅  最后修改于: 2021-10-27 08:55:09             🧑  作者: Mango

给定一个项目数组,第 i 个索引元素表示项目 id,并给定一个数字 m,任务是删除 m 个元素,以便应留下最小的不同 id。打印不同 id 的数量。
例子:

Input : arr[] = { 2, 2, 1, 3, 3, 3} 
            m = 3
Output : 1
Remove 1 and both 2's.So, only 3 will be 
left that's why distinct id is 1.

Input : arr[] = { 2, 4, 1, 5, 3, 5, 1, 3} 
            m = 2
Output : 3
Remove 2 and 4 completely. So, remaining ids 
are 1, 3 and 5 i.e. 3

询问:摩根士丹利

1- 计算元素的出现次数并将它们存储在哈希中。
2- 对哈希进行排序。
3- 开始从哈希中删除元素。
4- 返回散列中剩余的值数。

C++
// C++ program for above implementation
#include 
using namespace std;
 
// Function to find distintc id's
int distinctIds(int arr[], int n, int mi)
{
    unordered_map m;
    vector > v;
    int count = 0;
 
    // Store the occurrence of ids
    for (int i = 0; i < n; i++)
        m[arr[i]]++;
 
    // Store into the vector second as first and vice-versa
    for (auto it = m.begin(); it != m.end(); it++)
        v.push_back(make_pair(it->second, it->first));
 
    // Sort the vector
    sort(v.begin(), v.end());
 
    int size = v.size();
 
    // Start removing elements from the beginning
    for (int i = 0; i < size; i++) {
 
        // Remove if current value is less than
        // or equal to mi
        if (v[i].first <= mi) {
            mi -= v[i].first;
            count++;
        }
 
        // Return the remaining size
        else
            return size - count;
    }
    return size - count;
}
 
// Driver code
int main()
{
    int arr[] = { 2, 3, 1, 2, 3, 3 };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    int m = 3;
 
    cout << distinctIds(arr, n, m);
    return 0;
}


Java
//Java program for Minimum number of
//distinct elements after removing m items
import java.util.HashMap;
import java.util.Map;
import java.util.Map.Entry;
 
public class DistinctIds
{
    // Function to find distintc id's
    static int distinctIds(int arr[], int n, int mi)
    {
 
        Map m = new HashMap();
        int count = 0;
        int size = 0;
 
        // Store the occurrence of ids
        for (int i = 0; i < n; i++)
        {
 
            // If the key is not add it to map
            if (m.containsKey(arr[i]) == false)
            {
                m.put(arr[i], 1);
                size++;
            }
 
            // If it is present then increase the value by 1
            else m.put(arr[i], m.get(arr[i]) + 1);
        }
 
        // Start removing elements from the beginning
        for (Entry mp:m.entrySet())
        {
            // Remove if current value is less than
            // or equal to mi
            if (mp.getKey() <= mi)
            {
                mi -= mp.getKey();
                count++;
            }
            // Return the remaining size
            else return size - count;
        }
 
        return size - count;
    }
 
    //Driver method to test above function
    public static void main(String[] args)
    {
        // TODO Auto-generated method stub
        int arr[] = {2, 3, 1, 2, 3, 3};
        int m = 3;
 
        System.out.println(distinctIds(arr, arr.length, m));
    }
}
//This code is contributed by Sumit Ghosh


Python3
# Python program for above implementation
 
# Function to find distintc id's
def distinctIds(arr, n, mi):
  m = {}
  v = []
  count = 0
 
  # Store the occurrence of ids
  for i in range(n):
    if arr[i] in m:
      m[arr[i]] += 1
    else:
      m[arr[i]] = 1
 
  # Store into the list value as key and vice-versa
  for i in m:
    v.append([m[i],i])
 
  v.sort()
  size = len(v)
 
  # Start removing elements from the beginning
  for i in range(size):
     
    # Remove if current value is less than
    # or equal to mi
    if (v[i][0] <= mi):
      mi -= v[i][0]
      count += 1
         
    else:   # Return the remaining size
      return size - count
  return size - count
 
# Driver code
arr = [ 2, 3, 1, 2, 3, 3 ]
n = len(arr)
 
m = 3
 
# To display the result
print(distinctIds(arr, n, m))
 
# This code is contributed by rohitsingh07052


C#
// C# program for Minimum number of
// distinct elements after removing m items
using System;
using System.Collections.Generic; 
class GFG
{
 
  // Function to find distintc id's
  static int distinctIds(int[] arr, int n, int mi)
  {
 
    Dictionary m = new Dictionary(); 
    int count = 0;
    int size = 0;
 
    // Store the occurrence of ids
    for (int i = 0; i < n; i++)
    {
 
      // If the key is not add it to map
      if (m.ContainsKey(arr[i]) == false)
      {
        m[arr[i]] = 1;
        size++;
      }
 
      // If it is present then increase the value by 1
      else
      {
        m[arr[i]]++;
      }
    }
 
    // Start removing elements from the beginning
    foreach(KeyValuePair mp in m)
    {
 
      // Remove if current value is less than
      // or equal to mi
      if (mp.Key <= mi)
      {
        mi -= mp.Key;
        count++;
      }
    }
    return size - count;
  }
 
  // Driver code
  static void Main()
  {
 
    // TODO Auto-generated method stub
    int[] arr = {2, 3, 1, 2, 3, 3};
    int m = 3;
 
    Console.WriteLine(distinctIds(arr, arr.Length, m));
  }
}
 
// This code is contributed by divyeshrabadiya07


Javascript


输出:

1

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