-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSolution.java
More file actions
35 lines (32 loc) · 845 Bytes
/
Copy pathSolution.java
File metadata and controls
35 lines (32 loc) · 845 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
import java.util.HashMap;
import java.util.Map;
class Solution
{
public int minSetSize(int[] arr)
{
final int n = arr.length;
final Map<Integer, Integer> counts = new HashMap<>();
for (int num : arr)
{
counts.put(num, counts.getOrDefault(num, 0) + 1);
}
final int[] absCounter = new int[n + 1];
for (int num : counts.keySet())
{
absCounter[counts.get(num)]++;
}
int setSize = 0;
int newSize = 0;
for (int i = n; i >= 0 && newSize < n / 2; i--)
{
int numberOfSets = absCounter[i];
while (newSize < n / 2 && numberOfSets > 0)
{
setSize++;
newSize += i;
numberOfSets--;
}
}
return setSize;
}
}