File tree Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Expand file tree Collapse file tree 1 file changed +40
-0
lines changed Original file line number Diff line number Diff line change
1
+ '''
2
+ Bucket Sort ์ ๊ทผ:
3
+ 1. Counter๋ก ๊ฐ ์ซ์์ ๋น๋๋ฅผ ๊ณ์ฐํ๋ค.
4
+ 2. ๋น๋ ์๋ฅผ ์ธ๋ฑ์ค๋ก ํ๋ ๋ฒํท(buckets)์ ์์ฑํ๋ค.
5
+ - buckets[i]์๋ ๋ฑ์ฅ ํ์๊ฐ i๋ฒ์ธ ์ซ์๋ค์ด ๋ค์ด๊ฐ๋ค.
6
+ 3. ๋ฒํท์ ๊ฐ์ฅ ๋์ ๋น๋ ์ธ๋ฑ์ค๋ถํฐ ์ญ์์ผ๋ก ์ํํ๋ฉฐ
7
+ ์ซ์๋ฅผ ํ๋์ฉ ๊ฒฐ๊ณผ ๋ฆฌ์คํธ(result)์ ์ถ๊ฐํ๋ค.
8
+ 4. ๊ฒฐ๊ณผ ๋ฆฌ์คํธ์ k๊ฐ๊ฐ ๋ชจ์ด๋ฉด ๋ฐํํ๋ค.
9
+
10
+ ์๊ฐ ๋ณต์ก๋: O(n)
11
+ - ๋น๋ ๊ณ์ฐ O(n)
12
+ - ๋ฒํท ์ฑ์ฐ๊ธฐ O(n)
13
+ - ๊ฒฐ๊ณผ ์์ง O(n)
14
+
15
+ ๊ณต๊ฐ ๋ณต์ก๋: O(n)
16
+ - Counter ๋์
๋๋ฆฌ์ ๋ฒํท ๋ฆฌ์คํธ ์ฌ์ฉ
17
+ '''
18
+
19
+
20
+ from typing import List
21
+ from collections import Counter
22
+
23
+
24
+ class Solution :
25
+ def topKFrequent (self , nums : List [int ], k : int ) -> List [int ]:
26
+ freq = Counter (nums )
27
+
28
+ buckets = [[] for _ in range (len (nums ) + 1 )]
29
+ for num , count in freq .items ():
30
+ buckets [count ].append (num )
31
+
32
+ result = []
33
+ for i in range (len (buckets ) - 1 , 0 , - 1 ):
34
+ for num in buckets [i ]:
35
+ result .append (num )
36
+
37
+ if len (result ) == k :
38
+ return result
39
+
40
+ return result
You canโt perform that action at this time.
0 commit comments