← 返回 snapchat 的题目列表Top K Frequent Elements in Integer Array Efficiently
类型:online_judge
snapchat
Given a non-empty array of integers, return the k most frequent elements.
Input: An integer array containing n numbers and an integer k, indicating to return the top k frequent elements.
Output: An array containing the k most frequent integers.
Note:
You may assume k is always valid, 1 ≤ k ≤ the number of unique elements.
Your algorithm's time complexity must be better than O(n log n), where n is the array's size.
How to optimize if k is very large, e.g., close to n?
Example:
Input: nums = [1,1,1,2,2,3], k = 2
Output: [1, 2]
Input: nums = [1], k = 1
Output: [1]
The solution should consider the case when dealing with extremely large data files.
Example
Input
[1,1,1,2,2,3]
2