← 返回 meta 的题目列表Return Top K Frequent Elements Without Using Most Common
类型:online_judge
Given a non-empty array of integers, return the top k most frequent elements. You cannot use Python's built-in most_common method. Implement the function List[int] top_k_frequent(ints: List[int], k: int).
Example:
Input: [1,1,1,2,2,3], k = 2 Output: [1,2]
Input: [1], k = 1 Output: [1]
Consider the time complexity and ensure your implementation is efficient.
Example
Input
3
'1 1 1 2 2 3' 2
'1' 1
'1 2 3 4 5 5' 1