69. Kth Largest Element in an Array
Imagine sorting all input occurrences from largest to smallest. Return the value at rank k, where k equals one means the maximum.
This is not the kth distinct value. Repeated occurrences each occupy a rank, and the original array must follow the documented mutation rules.
Example 1
Input
nums=[3,2,1,5,6,4], k=2
Output
5
Explanation: Only 6 is strictly greater than 5.
Example 2
Input
nums=[5,5,4], k=2
Output
5
Explanation: Duplicate occurrences occupy separate ranks.
Constraints
1 <= nums.length <= 105-231 <= nums[i] <= 231 - 11 <= k <= nums.length
Hints
Hint 1
What information can be discarded when only k large occurrences matter?
Hint 2
Make the least valuable retained candidate easy to remove.
Hint 3
Maintain a min-heap of at most k occurrences; return its root.
Submissions
Your recent submissions for this problem.
Checking sign-in…
Discussion
Ask a question or share an approach. Posts are public; avoid personal information and mark solution spoilers clearly.
No discussion yet. Start the conversation with a question about this problem.
Checking sign-in…
Run your code to check the sample cases, or submit it to check the full validation suite.
Retain only the top two
[3,1,5,2,4]
2
4