Return the k-th largest value in nums: the largest is 1st, the next is 2nd, and so on. nums is not sorted. When a value repeats, each copy takes its own rank.
Example 1
Input
nums = [9, 4, 7, 1, 8], k = 2
Output
8
Explanation
from the largest down the values are 9, 8, 7, 4, 1, so the 2nd is 8.
Example 2
Input
nums = [5, 3, 5, 2, 5, 1], k = 3
Output
5
Explanation
from the largest down the values are 5, 5, 5, 3, 2, 1. The three 5s take ranks 1, 2 and 3, so the 3rd value is 5.
Constraints:
1 <= k <= nums.length <= 2 * 10^5
-10^9 <= nums[i] <= 10^9
Follow-up: sorting everything costs O(n log n). Can you do better on average?
JavaScript
Tab indents. Press Esc, then Tab to leave the editor.
Ctrl or ⌘ + Enter runs the tests.
Run your code to see every test here. Nothing is submitted or recorded.