Skip to content

Commit

Permalink
Merge pull request neetcode-gh#3490 from Tetsuya3850/patch-3
Browse files Browse the repository at this point in the history
  • Loading branch information
felivalencia3 authored Jun 16, 2024
2 parents 5d9a648 + f66299c commit 5d638cd
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions java/1481-least-number-of-unique-integers-after-k-removals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class Solution {
public int findLeastNumOfUniqueInts(int[] arr, int k) {
Map<Integer, Integer> freq = new HashMap<>();
for (int num : arr) {
freq.put(num, freq.getOrDefault(num, 0) + 1);
}
int[] freqList = new int[arr.length + 1];
for (int f : freq.values()) {
freqList[f] += 1;
}

int res = freq.size();
for (int f = 1; f < freqList.length; f++) {
int remove = freqList[f];
if (k >= f * remove) {
k -= f * remove;
res -= remove;
} else {
remove = k / f;
res -= remove;
break;
}
}
return res;
}
}

0 comments on commit 5d638cd

Please sign in to comment.