← 返回 meta 的题目列表Top K Largest Elements When N >> K
类型:online_judge
Given an integer array arr of length N and an integer k (with N >> k), find the largest k elements in the array.
Requirements:
Use an algorithm suitable for the N >> k setting.
You may return the k elements in any order (sorting is not required unless explicitly clarified with the interviewer).
Suggested constraints
1 <= N <= 1e7
1 <= k <= min(N, 1e5) and typically k << N
arr[i] fits in 32-bit signed integer
Sample tests
arr = [3, 2, 1, 5, 6, 4], k = 2 -> e.g. [6, 5]
arr = [5, -1, 5, 2, 0], k = 3 -> e.g. [5, 5, 2]
arr = [1], k = 1 -> [1]
arr = [2, 2, 2, 2], k = 2 -> [2, 2]
arr = [9, 8, 7, 6, 5], k = 5 -> [9, 8, 7, 6, 5]
Example
Input
6 2
3 2 1 5 6 4
Output
6 5