Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit27c4b6e

Browse files
authored
Update Least Number of Unique Integers after K Removals.java
1 parent93ab03a commit27c4b6e

File tree

1 file changed

+16
-25
lines changed

1 file changed

+16
-25
lines changed
Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,19 @@
11
classSolution {
2-
publicintfindLeastNumOfUniqueInts(int[]arr,intk) {
3-
Map<Integer,Integer>map =newHashMap<>();
4-
List<Integer>list =newArrayList<>();
5-
for (intnum :arr) {
6-
if (!map.containsKey(num)) {
7-
list.add(num);
8-
}
9-
map.put(num,map.getOrDefault(num,0) +1);
2+
publicintfindLeastNumOfUniqueInts(int[]arr,intk) {
3+
Map<Integer,Integer>map =newHashMap<>();
4+
for (intnum :arr) {
5+
map.put(num,map.getOrDefault(num,0) +1);
6+
}
7+
PriorityQueue<Integer>pq =newPriorityQueue<>((a,b) ->map.get(a).compareTo(map.get(b)));
8+
pq.addAll(map.keySet());
9+
while (k >0 && !pq.isEmpty()) {
10+
intcount =map.get(pq.peek());
11+
if (count >k) {
12+
break;
13+
}
14+
k -=count;
15+
pq.poll();
16+
}
17+
returnpq.size();
1018
}
11-
Collections.sort(list,newComparator<Integer>(){
12-
publicintcompare(Integero1,Integero2) {
13-
returnmap.get(o1) -map.get(o2);
14-
}
15-
});
16-
for (Integernum :list) {
17-
intcount =map.get(num);
18-
k -=count;
19-
if (k >=0) {
20-
map.remove(num);
21-
}
22-
else {
23-
break;
24-
}
25-
}
26-
returnmap.size();
27-
}
2819
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp