Kth Largest Element in an Array
Medium Heap / Priority Queue AI round
nums is a list of whole numbers, and k is a whole number from 1 to the length of nums.
Picture nums in order from the largest number down to the smallest, keeping every copy of a number that appears more than once. The k-th largest number is the one at place k in that order, counting from 1.
Return the k-th largest number of nums.
Example 1
Input: nums = [7,-2,15,4,9,4], k = 3
Output: 7
In order from the largest down the numbers are 15, 9, 7, 4, 4, -2, and the third of them is 7. Both copies of 4 keep their places: 4 is both the 4th and the 5th largest.
Example 2
Input: nums = [8,8,3,8], k = 2
Output: 8
Example 3
Input: nums = [10,-10,0], k = 3
Output: -10
Constraints
1 ≤ k ≤ nums.length ≤ 2 × 10⁵-10⁹ ≤ nums[i] ≤ 10⁹
Practice this in an AI round
30 minutes on this problem with an AI agent beside your code, then a report on your solution and how you used the agent.
Your first AI round is free.
More Heap / Priority Queue
- Top K Frequent ElementsAI round · Medium
- Reorganize StringAI round · Medium
- Task SchedulerMedium
- Sort Characters By FrequencyMedium
- K Closest Points to OriginMedium