Imagine you're organizing a sports tournament and need to quickly find the k-th best performer from a list of scores. This problem asks you to find the k-th largest element in an unsorted integer array.
Given an integer array nums and an integer k, return the k-th largest element in the array. Note that we want the k-th largest element in sorted order, not the k-th distinct element.
Challenge: Can you solve this without fully sorting the array? There are elegant solutions that can do better than O(n log n) time complexity!
Example: In array [3,2,1,5,6,4] with k=2, the 2nd largest element is 5 (since sorted array would be [6,5,4,3,2,1]).
Input & Output
Visualization
Time & Space Complexity
Average case: each partition reduces problem size by half. Worst case: poor pivot choices lead to O(nยฒ)
In-place algorithm, only uses constant extra space for recursion variables
Constraints
- 1 โค k โค nums.length โค 105
- -104 โค nums[i] โค 104
- k is guaranteed to be valid (within array bounds)