File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ /**
2+ * Runtime: 15ms
3+ * Time Complexity: O(n log n)
4+ * - ๋น๋ ๊ณ์ฐ: O(n)
5+ * - ์ ๋ ฌ: O(u log u) โค O(n log n)
6+ * - k๊ฐ ์ถ์ถ: O(k)
7+ *
8+ * Memory: 47.73MB
9+ * Space Complexity: O(n)
10+ * - HashMap: O(u) โค O(n)
11+ * - List: O(u) โค O(n)
12+ *
13+ * Approach: HashMap์ผ๋ก ๋น๋ ๊ณ์ฐ ํ ์ ๋ ฌํ์ฌ top k ์ถ์ถ
14+ * - ๋ฐฐ์ด ์ํํ์ฌ ๊ฐ ์์์ ๋น๋ ๊ณ์ฐ
15+ * - ๋น๋ ๊ธฐ์ค ๋ด๋ฆผ์ฐจ์ ์ ๋ ฌ
16+ * - ์์ k๊ฐ ์์ ์ถ์ถ
17+ */
18+ class Solution {
19+ public int [] topKFrequent (int [] nums , int k ) {
20+ Map <Integer , Integer > frequentMap = new HashMap <>();
21+ for (int num : nums ) {
22+ frequentMap .put (num , frequentMap .getOrDefault (num , 0 )+1 );
23+ }
24+
25+ List <Map .Entry <Integer , Integer >> list = new ArrayList <>(frequentMap .entrySet ());
26+ list .sort ((a , b ) -> b .getValue () - a .getValue ());
27+
28+ int [] result = new int [k ];
29+ for (int i =0 ; i <k ; i ++) {
30+ result [i ] = list .get (i ).getKey ();
31+ }
32+
33+ return result ;
34+ }
35+ }
You canโt perform that action at this time.
0 commit comments