Created
April 1, 2024 02:06
-
-
Save unilecs/44954a4bb4fc6c75711a2de690d1b240 to your computer and use it in GitHub Desktop.
Задача: Наименьшее количество различных целых чисел после K удалений
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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