Skip to content
BytePatterns

Kth Largest Value

MediumHeaps#min-heap#top-k~25m

Problem

Given an unsorted list of numbers and a rank k, return the value that would sit in position k if the list were arranged from largest to smallest. Ranking is by value, so repeated values each occupy their own rank. You may assume k is at least 1 and never exceeds the length of the list.

Examples

Input:  nums = [3, 2, 1, 5, 6, 4], k = 2
Output: 5
Why:    ordered from the top the values run 6, 5, 4, 3, 2, 1
Input:  nums = [7, 7, 7], k = 2
Output: 7
Why:    duplicates each take a rank of their own
Input:  nums = [9], k = 1
Output: 9
Why:    edge case, the only value is also the largest

Hints

0 / 3

Stuck on the idea rather than the code? Top K Elements covers it.