beginner · heap · top-k · priority queue
The k-th highest score
Problem
Scores arrive one by one. Return the k-th highest score among all of them, counting repeated scores separately (in [9, 9, 4], the 2nd highest is 9).
Constraints
- 1 ≤ k ≤ n ≤ 32
- scores from −999 to 999
Examples
scores = [5, 12, 3, 8, 12, 7], k = 3 → 8 — Highest first: 12, 12, 8, …
Show the solution
Min-heap of size k
The k-th highest is the smallest member of the top-k club. A min-heap keeps that member on top, ready to be compared with or replaced.
Why it is correct
Invariant: the heap holds the k highest scores seen so far (as a multiset), or all of them while fewer than k have arrived. Pushing a score and popping the minimum of k + 1 removes a score that k others are at least as high as, so it cannot be among the k highest. At the end, the minimum of the k highest is by definition the k-th highest.
Complexity
Time O(n log k), extra space O(k). n pushes and at most n − k pops on a heap of at most k + 1 elements.
For contrast: Sort descending and take position k − 1. That costs O(n log n) time and O(n) space.
Edge cases
- k = 1: the maximum.
- k = n: the minimum.
- Repeated scores count separately.
Common mistakes
- Using a max-heap of everything and popping k times (O(n + k log n), fine, but O(n) memory for a stream).
- Removing duplicates, which changes the rank.
Step through it
The solution in four languages
Each is checked against one shared set of cases and a randomized comparison with a brute-force reference.
namespace Bekadan.Dsa.Examples.Problems;
public static class KthLargestValue
{
/// <summary>The k-th highest score, repeats counted (1 <= k <= n). Min-heap of size k: O(n log k) time, O(k) space.</summary>
public static int Find(int[] scores, int k)
{
var heap = new PriorityQueue<int, int>(); // the k highest so far; the smallest of them on top
foreach (int s in scores)
{
heap.Enqueue(s, s);
if (heap.Count > k) heap.Dequeue(); // the smallest of k + 1 cannot be among the k highest
}
return heap.Peek();
}
}
Tested on .NET 10 (SDK 10.0.403), C# 14 · examples/csharp/Bekadan.Dsa.Examples/Problems/KthLargestValue.cs
import heapq
from collections.abc import Iterable
def find(scores: Iterable[int], k: int) -> int:
"""The k-th highest score, repeats counted (1 <= k <= n). Min-heap of size k: O(n log k) time, O(k) space."""
heap: list[int] = [] # the k highest so far; the smallest of them on top
for s in scores:
heapq.heappush(heap, s)
if len(heap) > k:
heapq.heappop(heap) # the smallest of k + 1 cannot be among the k highest
return heap[0]
Tested on CPython 3.14.0 · examples/python/dsa_examples/problems/kth_largest_value.py
package dev.bekadan.dsa.problems;
import java.util.PriorityQueue;
public final class KthLargestValue {
private KthLargestValue() {
}
/** The k-th highest score, repeats counted (1 <= k <= n). Min-heap of size k: O(n log k) time, O(k) space. */
public static int find(int[] scores, int k) {
PriorityQueue<Integer> heap = new PriorityQueue<>(); // the k highest so far; the smallest of them on top
for (int s : scores) {
heap.add(s);
if (heap.size() > k) {
heap.poll(); // the smallest of k + 1 cannot be among the k highest
}
}
return heap.peek();
}
}
Tested on Java 25 (Temurin 25.0.5); also compiles on Java 17 · examples/java/src/main/java/dev/bekadan/dsa/problems/KthLargestValue.java
#pragma once
#include <cstddef>
#include <functional>
#include <queue>
#include <span>
#include <vector>
namespace bekadan::dsa::problems {
// The k-th highest score, repeats counted (1 <= k <= n). Min-heap of size k: O(n log k) time, O(k) space.
inline int kth_largest_value(std::span<const int> scores, std::size_t k) {
std::priority_queue<int, std::vector<int>, std::greater<>> heap; // the k highest so far; the smallest on top
for (const int s : scores) {
heap.push(s);
if (heap.size() > k) heap.pop(); // the smallest of k + 1 cannot be among the k highest
}
return heap.top();
}
} // namespace bekadan::dsa::problems
Tested on C++23, GCC 15.3 (libstdc++) · examples/cpp/include/bekadan/dsa/problems/kth_largest_value.hpp