← 返回 meta 的题目列表Quickselect: Find the K-th element (with large-scale follow-up)
类型:online_judge
Problem
Given an integer array nums of length n (may contain duplicates) and an integer k, return the k-th largest element in the array (k-th by value order, not the k-th distinct element).
Requirements
Implement an average O(n) solution (e.g., using Quickselect).
Follow-up (very large data): when n is extremely large (cannot fit into memory at once) or you need more stable performance, describe/implement an alternative (e.g., using a heap).
I/O Format
Input:
Line 1: integer n
Line 2: n integers (the array)
Line 3: integer k
Output:
One line: the k-th largest element
Constraints
1 <= n <= 2 * 10^5
-10^9 <= nums[i] <= 10^9
1 <= k <= n
Example
Input:
6
3 2 1 5 6 4
2
Output:
5
Example
Input
6
3 2 1 5 6 4
2
Output
5