File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ // ๊ฐ ์์ ๊ฐ์๋ฅผ ์นด์ดํธ ํ๊ณ ์นด์ดํธ ํ ๊ฐ์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ ์ฐ์ ์์ ํ๋ก
2+ // k๊ฐ๋ฅผ ์ถ์ถํ์ฌ result ๋ฆฌ์คํธ์ ๋ด๋๋ค.
3+
4+ // ์๊ฐ๋ณต์ก๋ : O(NlogN)
5+ // ๊ณต๊ฐ๋ณต์ก๋ : O(N)
6+
7+
8+ public class SolutionGotprgmer {
9+
10+ public int [] topKFrequent (int [] nums , int k ) {
11+ Map <Integer , Integer > map = new HashMap <>();
12+ for (int num :nums ){
13+ map .put (num ,map .getOrDefault (num ,0 )+1 );
14+ }
15+ PriorityQueue <Map .Entry <Integer , Integer >> pq = new PriorityQueue <>(
16+ (e1 , e2 ) -> e2 .getValue ().compareTo (e1 .getValue ())
17+ );
18+ for (Map .Entry <Integer ,Integer > entry : map .entrySet ()){
19+ pq .offer (entry );
20+ }
21+
22+ int [] result = new int [k ];
23+ for (int ansIdx =0 ;ansIdx < k ; ansIdx ++){
24+ result [ansIdx ] = pq .poll ().getKey ();
25+ }
26+ return result ;
27+ }
28+ }
You canโt perform that action at this time.
0 commit comments