mirror of
https://github.com/autistic-symposium/master-algorithms-py.git
synced 2025-05-25 01:41:20 -04:00
👾
This commit is contained in:
parent
1d44d182e2
commit
a85ed914d3
320 changed files with 0 additions and 0 deletions
|
@ -1,17 +0,0 @@
|
|||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
# author: bt3gl
|
||||
|
||||
def top_k_frequentnums: list[int], k: int) -> list[int]:
|
||||
|
||||
# O(1) time
|
||||
if k == len(nums):
|
||||
return nums
|
||||
|
||||
# 1. build a hashmap element: frequency
|
||||
counter = Counter(nums)
|
||||
|
||||
# 2. build a heap of k most frequent elements
|
||||
# 3. build an output array
|
||||
# O(N log k) time
|
||||
return heapq.nlargest(k, counter.keys(), key=counter.get)
|
Loading…
Add table
Add a link
Reference in a new issue