Skip to content

Commit c991a2d

Browse files
committed
feat: week1-top k frequent elements
1 parent f002107 commit c991a2d

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
from typing import List
2+
3+
'''
4+
[1์ฐจ ์‹œ๋„]
5+
Approach: nums ๋ฆฌ์ŠคํŠธ์˜ ๊ฐ ์›์†Œ์˜ ๋นˆ๋„์ˆ˜๋ฅผ hash_map์— ์ €์žฅํ›„, ๋นˆ๋„์ˆ˜ ๊ธฐ์ค€์œผ๋กœ ์ •๋ ฌ์„ ํ•˜๊ณ  ์ƒ์œ„ k๊ฐœ๋ฅผ ๋ฐ˜ํ™˜
6+
count ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•ด์„œ ๊ฐ„๊ฒฌํ• ๊ฒŒ ์ฝ”๋“œ๋ฅผ ์™„์„ฑํ•˜๊ณ ์ž ํ–ˆ์Šต๋‹ˆ๋‹ค.
7+
8+
Time Complexity: O(nยฒ)
9+
- for num in nums ์ˆœํ™˜๊ณผ nums.count(num) ์˜ ์ค‘์ฒฉ์œผ๋กœ ์ธํ•ด O(nยฒ) ๋ฐœ์ƒ
10+
11+
Space Complexity: O(n)
12+
- hash_map ์ €์žฅ ๊ณต๊ฐ„๊ณผ sorted_hash_map ์ €์žฅ ๊ณต๊ฐ„์œผ๋กœ ์ธํ•ด O(n) ๋ฐœ์ƒ
13+
14+
-
15+
'''
16+
class Solution:
17+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
18+
hash_map = {}
19+
for num in nums:
20+
hash_map[num] = nums.count(num)
21+
22+
# hash_map ์ •๋ ฌ
23+
sorted_hash_map = sorted(hash_map.items(), key=lambda x: x[1], reverse=True)
24+
# ์ƒ์œ„ k๊ฐœ ๋ฐ˜ํ™˜
25+
return [x[0] for x in sorted_hash_map[:k]]
26+
27+
'''
28+
[2์ฐจ ์‹œ๋„]
29+
Approach: ์œ„์˜ ๋ฐฉ์‹์—์„œ count ํ•จ์ˆ˜๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , hash_map์— ๋นˆ๋„์ˆ˜๋ฅผ ์ง์ ‘ ์นด์šดํŒ… ํ•˜๋„๋ก ์ˆ˜์ •ํ•˜๋„๋ก ํ•ด์„œ
30+
Time Complexity๋ฅผ ๊ฐœ์„ ํ•˜๊ธฐ ์œ„ํ•ด ๋…ธ๋ ฅํ–ˆ์Šต๋‹ˆ๋‹ค.
31+
32+
Time Complexity: O(n log n)
33+
- for num in nums ์ˆœํ™˜์— O(n) ๋ฐœ์ƒ
34+
- hash_map ์ •๋ ฌ์— O(m log m)์œผ๋กœ ์˜ˆ์ธก ๋˜๋ฉฐ ์ตœ๋Œ€ m =< n ์ด๋ฏ€๋กœ O(n log n) ๋ฐœ์ƒ
35+
36+
'''
37+
class Solution:
38+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
39+
hash_map = {}
40+
for num in nums:
41+
if hash_map.get(num):
42+
hash_map.update({num: hash_map[num]+1})
43+
else:
44+
hash_map.update({num: 1})
45+
46+
# hash_map ์ •๋ ฌ
47+
sorted_hash_map = sorted(hash_map.items(), key=lambda x: x[1], reverse=True)
48+
# ์ƒ์œ„ k๊ฐœ ๋ฐ˜ํ™˜
49+
return [x[0] for x in sorted_hash_map[:k]]
50+
51+
52+
'''
53+
[3์ฐจ ์‹œ๋„ - Bucket Sort ํ™œ์šฉ]
54+
Approach: Bucket Sort ๋ฐฉ์‹์„ ํ™œ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋์Šต๋‹ˆ๋‹ค.
55+
for loop์™€ ์กฐ๊ฑด๋ฌธ์ด ๋ฐ˜๋ณต๋˜๊ธฐ๋„ ํ•˜์ง€๋งŒ, ๋ฐฐ์—ด์˜ ํฌ๊ธฐ์— ๋งž์ถฐ ํ•œ๋ฒˆ์”ฉ๋งŒ ์ˆœํšŒํ•˜๊ธฐ ๋•Œ๋ฌธ์—
56+
Time Complexity๋ฅผ O(n)์œผ๋กœ ๊ฐœ์„ ํ•  ์ˆ˜ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
57+
58+
Time Complexity: O(n)
59+
- for num in nums ์ˆœํ™˜ํ•˜๋ฉฐ ๋นˆ๋„์ˆ˜ ์นด์šดํŒ…: O(n)
60+
- ๋นˆ๋„์— ๋”ฐ๋ผ ์ˆซ์ž๋“ค์„ ๋ฒ„ํ‚ท์— ๋„ฃ๊ธฐ: O(n)
61+
- ๋ฒ„ํ‚ท์„ ๋’ค์—์„œ๋ถ€ํ„ฐ ์ˆœํšŒํ•˜๋ฉฐ ์ƒ์œ„ k๊ฐœ ์ˆซ์ž ์ฑ„์šฐ๊ธฐ: O(n)
62+
63+
Space Complexity: O(n)
64+
- freq ๋”•์…”๋„ˆ๋ฆฌ์™€ bucket ๋ฆฌ์ŠคํŠธ๋กœ ์ธํ•ด O(n) ๋ฐœ์ƒ
65+
'''
66+
class Solution:
67+
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
68+
hash_map = {}
69+
for num in nums:
70+
hash_map[num] = hash_map.get(num, 0) + 1
71+
72+
# ์ธ๋ฑ์Šค = ๋“ฑ์žฅ ํšŸ์ˆ˜, ๊ฐ’ = ๊ทธ ํšŸ์ˆ˜๋งŒํผ ๋“ฑ์žฅํ•œ ์ˆซ์ž ๋ฆฌ์ŠคํŠธ
73+
bucket = [[] for _ in range(len(nums) + 1)]
74+
for num, count in hash_map.items():
75+
bucket[count].append(num)
76+
77+
result: List[int] = []
78+
# ๊ฐ€์žฅ ํฐ ๋นˆ๋„๋ถ€ํ„ฐ ๋‚ด๋ ค์˜ค๋ฉด์„œ ์ˆซ์ž ์ˆ˜์ง‘ (for ๋ฃจํ”„ ์—ญ์ˆœ)
79+
for count in range(len(bucket) - 1, -1, -1):
80+
if not bucket[count]:
81+
continue
82+
83+
for num in bucket[count]:
84+
result.append(num)
85+
if len(result) == k:
86+
return result
87+
88+
return result

0 commit comments

Comments
ย (0)