← 返回 uber 的题目列表Top-K Using a Priority Queue (Phone Screen)
类型:online_judge
Prompt: Top-K (Priority Queue)
Given an integer array nums and an integer k, return the largest k elements in the array (any order).
Input
Line 1: integer n (array length)
Line 2: n integers nums[i]
Line 3: integer k
Output
Print k integers: the largest k elements from nums, in any order, separated by spaces.
Constraints
1 <= n <= 2 * 10^5
-10^9 <= nums[i] <= 10^9
1 <= k <= n
Expected time complexity: O(n log k)
Example
Input:
n = 6
nums = 3 2 1 5 6 4
k = 2
Output:
5 6
Example
Input
6
3 2 1 5 6 4
2
Output
5 6