Picture nums sorted from biggest to smallest, and count along to position k (the first position is 1). Return the number at that spot: the k-th largest.
- Repeated numbers each take their own position. In
[5, 5, 1], positions 1 and 2 are both5. kis at least 1 and at mostnums.length.numscan hold up to 100,000 numbers, and some can be negative.- You can reorder
numsor work on a copy.
Input: nums = [7, 10, 4, 3, 20, 15], k = 3
Output: 10From largest to smallest: 20, 15, 10, …
Input: nums = [5, 5, 5, 1], k = 2
Output: 5