diff options
| author | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2024-09-27 23:44:50 +0900 |
|---|---|---|
| committer | TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> | 2024-09-27 23:44:50 +0900 |
| commit | 41337c5d4da862c72a4c849d4d70b5aef21fd881 (patch) | |
| tree | 7688431c21a9c9dd2a7a563e0a34bbe836efd80f /1_array_hashing | |
| parent | bfee35a21c60e062c0033ba4e7e032b86dcadf7c (diff) | |
Init
Diffstat (limited to '1_array_hashing')
| -rw-r--r-- | 1_array_hashing/top_k_elements.py | 9 |
1 files changed, 5 insertions, 4 deletions
diff --git a/1_array_hashing/top_k_elements.py b/1_array_hashing/top_k_elements.py index 2539caa..c237960 100644 --- a/1_array_hashing/top_k_elements.py +++ b/1_array_hashing/top_k_elements.py @@ -30,7 +30,7 @@ Constraints: 1 <= k <= number of distinct elements in nums. """ -from typing import List +from typing import Dict, List class Solution: @@ -49,20 +49,21 @@ class Solution: Returns: List[int]: elements list """ - count = {} - freq = [[] for _ in range(len(nums) + 1)] + count: Dict = {} + freq: List = [[] for _ in range(len(nums) + 1)] for n in nums: count[n] = 1 + count.get(n, 0) for n, c in count.items(): freq[c].append(n) - res = [] + res: List = [] for i in range(len(freq) - 1, 0, -1): for n in freq[i]: res.append(n) if len(res) == k: return res + return [] case1 = [1, 2, 2, 3, 3, 3] |
