From 41337c5d4da862c72a4c849d4d70b5aef21fd881 Mon Sep 17 00:00:00 2001 From: TheSiahxyz <164138827+TheSiahxyz@users.noreply.github.com> Date: Fri, 27 Sep 2024 23:44:50 +0900 Subject: Init --- 1_array_hashing/top_k_elements.py | 9 +++++---- 1 file 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] -- cgit v1.2.3