|
| 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