leetcode
leetcode copied to clipboard
Bug Report for top-k-elements-in-list
Bug Report for https://neetcode.io/problems/top-k-elements-in-list
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
class Solution: def topKFrequent(self, nums: List[int], k: int) -> List[int]: res = {} freq = [[] for i in range(len(nums) + 1)] for num in nums: res[num] = res.get(num, 0) + 1 for num, cnt in res.items(): freq[cnt].append(num) arr = [] for i in range(len(nums), 0, -1): -- here for num in freq[i]: arr.append(num) if len(arr) == k: return arr return arr
Could you describe the issue?