← 返回 microsoft 的题目列表Top K Largest Elements in an Array
类型:online_judge
Given an integer array nums of length N and an integer K, return the largest K elements in the array.
Requirements:
Implement a solution using a min-heap.
Target time complexity: O(N log K); extra space complexity: O(K).
Input
Line 1: two integers N and K.
Line 2: N integers representing nums.
Output
Print the largest K elements separated by spaces.
The output order does not matter.
Constraints
1 <= N <= 2 * 10^5
1 <= K <= N
-10^9 <= nums[i] <= 10^9
Example
Input
6 3
3 2 1 5 6 4
Output (one possible)
4 5 6
Example
Input
6 3
3 2 1 5 6 4
Output
4 5 6