← 返回 amazon 的题目列表Frequent K Elements and One Linear Scan
类型:online_judge
Problem
Given an array, find the top k elements with the most frequency using one linear scan.
Input Format
The first line contains two integers n and k, indicating the length of the array and the number of elements to be found.
The second line contains n integers, representing the elements of the array.
Output Format
Output an array containing the top k most frequent elements, sorted in descending order of frequency. If frequencies are the same, sort in ascending order of value.
Sample Input
6 2
1 1 1 2 2 3
Sample Output
[1, 2]
Constraints
1 <= k <= n <= 10^5
-10^4 <= elements of the array <= 10^4
Example
Input
6 2
1 1 1 2 2 3