-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1338. Reduce Array Size to The Half.java
More file actions
55 lines (41 loc) · 1.11 KB
/
1338. Reduce Array Size to The Half.java
File metadata and controls
55 lines (41 loc) · 1.11 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
class Solution {
public int minSetSize(int[] arr) {
HashMap<Integer, Integer> map = new HashMap<>();
for(int val : arr)
{
map.put(val, map.getOrDefault(val, 0)+1);
}
PriorityQueue<Pair> pq = new PriorityQueue<>();
for(int key : map.keySet())
{
pq.add(new Pair(key, map.get(key)));
}
int size = arr.length;
int result = 0;
while(size > arr.length/2)
{
Pair p = pq.poll();
size -= p.count;
result++;
}
return result;
}
class Pair implements Comparable<Pair>{
int val;
int count;
Pair(){}
Pair(int val, int count)
{
this.val = val;
this.count = count;
}
public int compareTo(Pair p)
{
return p.count - this.count;
}
public String toString()
{
return this.val + " - " + this.count;
}
}
}