← 返回 amazon 的题目列表Top K Frequent Elements
类型:online_judge
Problem Description
Given an integer array nums and an integer k, return the k most frequent elements.
You may return the answer in any order.
In the interview, you may first be asked to solve it with a brute-force/sorting approach, then optimize it using a heap / priority queue.
Input Format
For runnable testing, use the following input format:
n
nums[0] nums[1] ... nums[n-1]
k
Where:
n is the length of the array;
The second line contains n integers;
The third line contains integer k.
Output Format
Print the k most frequent elements separated by spaces. The order of elements does not matter.
For deterministic testing, the sample outputs below are shown in ascending order; in an interview, any valid order is usually acceptable.
Constraints
1 <= n <= 10^5
-10^9 <= nums[i] <= 10^9
1 <= k <= number of distinct elements in nums
The answer is guaranteed to be unique, or any valid ordering is accepted.
Example
Input
6
1 1 1 2 2 3
2
Output
1 2