Skip to content

Instantly share code, notes, and snippets.

@unilecs
Created April 1, 2024 02:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unilecs/44954a4bb4fc6c75711a2de690d1b240 to your computer and use it in GitHub Desktop.
Save unilecs/44954a4bb4fc6c75711a2de690d1b240 to your computer and use it in GitHub Desktop.
Задача: Наименьшее количество различных целых чисел после K удалений
using System;
using System.Linq;
using System.Collections.Generic;
public class Program
{
public static int FindMinNumOfUniqueIntegers(int[] arr, int k) {
var map = new Dictionary<int, int>();
for (int i = 0; i < arr.Length; i++)
{
if (!map.ContainsKey(arr[i]))
{
map[arr[i]] = 0;
}
map[arr[i]] += 1;
}
int count = 0;
var sortedFreq = map.OrderBy(x => x.Value).Select(x => x.Value);
foreach (int freq in sortedFreq)
{
if (freq > k)
{
break;
}
k -= freq;
count++;
}
return sortedFreq.Count() - count;
}
public static void Main()
{
Console.WriteLine("UniLecs");
// tests
Console.WriteLine(FindMinNumOfUniqueIntegers(new int[] { 5, 5, 4 }, 1).ToString()); // 1
Console.WriteLine(FindMinNumOfUniqueIntegers(new int[] { 4, 3, 1, 1, 3, 3, 2 }, 2).ToString()); // 2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment