Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions Algorithms/Medium/347_TopKFrequentElements/Solution.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class Solution:
def topKFrequent(self, nums: List[int], k: int) -> List[int]:
freq_arr = [ [] for _ in range(len(nums))]
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remove the redundant space before []

elem_freq_map = {}

for elem in nums:
elem_freq_map[elem] = elem_freq_map.get(elem, 0) + 1

for elem, idx in elem_freq_map.items():
freq_arr[idx - 1].append(elem)

res = []

for i in range(len(freq_arr) - 1, -1, -1):
if freq_arr[i]: res.extend(freq_arr[i])
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a blank line after this

if len(res) == k:
return res

return