- Notifications
You must be signed in to change notification settings - Fork2.4k
Open
Description
Bug Report forhttps://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
Metadata
Metadata
Assignees
Labels
No labels