InferenceAI

Enter at least two characters to search.

Start free

Kth Largest Value

mediumHeaps25 min25 XP

Return the k-th largest value in nums (counting duplicates).

Requirements

  • Aim for O(nlog⁡k)O(n \log k) with a size-kk min-heap.

Examples

EXAMPLE WALKTHROUGHsecond largest

A guided view of the published example, not a trace of your code.

INPUT[[3,2,1,5,6,4],2]
TASKkth_largest_value()
EXPECTED OUTPUT5

Start with the input values shown in this example.

Example 1 of 3

Input: nums = [3,2,1,5,6,4], k = 2

Output: 5

Explanation: second largest

Input: nums = [3,2,3,1,2,4,5,5,6], k = 4

Output: 4

Explanation: with duplicates

Input: nums = [1], k = 1

Output: 1

Explanation: single

Hint 1

Keep the k largest values seen so far in a min-heap; its top is the answer.

Topics

DSAHeapsPython