8.3 The top-k problem¶
Question
Given an unsorted array nums of length \(n\), return its largest \(k\) elements.
We first introduce two straightforward solutions, then a more efficient solution using a heap.
8.3.1 Method 1: Repeated selection¶
As shown below, scan the array \(k\) times, selecting the \(1\)st, \(2\)nd, \(\dots\), and \(k\)th largest elements in successive rounds. This takes \(O(nk)\) time.
This method is suitable only when \(k \ll n\). As \(k\) approaches \(n\), its time complexity approaches \(O(n^2)\), making it very slow.
Figure 8-6 Finding the largest k elements by repeated scans
Tip
When \(k = n\), this produces a fully sorted sequence and is equivalent to selection sort.
8.3.2 Method 2: Sorting¶
As shown below, sort nums and return its rightmost \(k\) elements. This takes \(O(n \log n)\) time.
This method does more work than necessary: we need only the largest \(k\) elements, not a sorted order for all the others.
Figure 8-7 Finding the largest k elements by sorting
8.3.3 Method 3: A heap¶
A heap solves the top-k problem more efficiently, following the steps below.
- Initialize a min heap, whose root holds its smallest element.
- Insert the first \(k\) array elements into the heap one by one.
- Starting with element \(k + 1\), if the current element is larger than the root, remove the root and insert the current element.
- After the traversal, the heap contains the largest \(k\) elements.
Figure 8-8 Finding the largest k elements with a heap
An implementation follows:
def top_k_heap(nums: list[int], k: int) -> list[int]:
"""Use a heap to find the k largest elements in an array"""
# Initialize a min heap
heap = []
# Push the first k array elements onto the heap
for i in range(k):
heapq.heappush(heap, nums[i])
# Starting with element k+1, keep the heap size at k
for i in range(k, len(nums)):
# If the current element is larger than the heap top, pop the top element and push the current element
if nums[i] > heap[0]:
heapq.heappop(heap)
heapq.heappush(heap, nums[i])
return heap
/* Use a heap to find the k largest elements in an array */
priority_queue<int, vector<int>, greater<int>> topKHeap(vector<int> &nums, int k) {
// Initialize a min heap
priority_queue<int, vector<int>, greater<int>> heap;
// Push the first k array elements onto the heap
for (int i = 0; i < k; i++) {
heap.push(nums[i]);
}
// Starting with element k+1, keep the heap size at k
for (int i = k; i < nums.size(); i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > heap.top()) {
heap.pop();
heap.push(nums[i]);
}
}
return heap;
}
/* Use a heap to find the k largest elements in an array */
Queue<Integer> topKHeap(int[] nums, int k) {
// Initialize a min heap
Queue<Integer> heap = new PriorityQueue<Integer>();
// Push the first k array elements onto the heap
for (int i = 0; i < k; i++) {
heap.offer(nums[i]);
}
// Starting with element k+1, keep the heap size at k
for (int i = k; i < nums.length; i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > heap.peek()) {
heap.poll();
heap.offer(nums[i]);
}
}
return heap;
}
/* Use a heap to find the k largest elements in an array */
PriorityQueue<int, int> TopKHeap(int[] nums, int k) {
// Initialize a min heap
PriorityQueue<int, int> heap = new();
// Push the first k array elements onto the heap
for (int i = 0; i < k; i++) {
heap.Enqueue(nums[i], nums[i]);
}
// Starting with element k+1, keep the heap size at k
for (int i = k; i < nums.Length; i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > heap.Peek()) {
heap.Dequeue();
heap.Enqueue(nums[i], nums[i]);
}
}
return heap;
}
/* Use a heap to find the k largest elements in an array */
func topKHeap(nums []int, k int) *minHeap {
// Initialize a min heap
h := &minHeap{}
heap.Init(h)
// Push the first k array elements onto the heap
for i := 0; i < k; i++ {
heap.Push(h, nums[i])
}
// Starting with element k+1, keep the heap size at k
for i := k; i < len(nums); i++ {
// If the current element is larger than the heap top, pop the top element and push the current element
if nums[i] > h.Top().(int) {
heap.Pop(h)
heap.Push(h, nums[i])
}
}
return h
}
/* Use a heap to find the k largest elements in an array */
func topKHeap(nums: [Int], k: Int) -> [Int] {
// Build a heap from the first k elements and use it as a min heap
var heap = Heap(nums.prefix(k))
// Starting with element k+1, keep the heap size at k
for i in nums.indices.dropFirst(k) {
// If the current element exceeds the heap minimum, remove the minimum and insert the current element
if nums[i] > heap.min()! {
_ = heap.removeMin()
heap.insert(nums[i])
}
}
return heap.unordered
}
/* Push an element onto the heap */
function pushMinHeap(maxHeap, val) {
// Negate the value
maxHeap.push(-val);
}
/* Pop an element from the heap */
function popMinHeap(maxHeap) {
// Negate the value
return -maxHeap.pop();
}
/* Access the top element of the heap */
function peekMinHeap(maxHeap) {
// Negate the value
return -maxHeap.peek();
}
/* Get the elements in the heap */
function getMinHeap(maxHeap) {
// Negate the value
return maxHeap.getMaxHeap().map((num) => -num);
}
/* Use a heap to find the k largest elements in an array */
function topKHeap(nums, k) {
// Initialize a min heap
// Note: negate all values in the heap to simulate a min heap with a max heap
const maxHeap = new MaxHeap([]);
// Push the first k array elements onto the heap
for (let i = 0; i < k; i++) {
pushMinHeap(maxHeap, nums[i]);
}
// Starting with element k+1, keep the heap size at k
for (let i = k; i < nums.length; i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > peekMinHeap(maxHeap)) {
popMinHeap(maxHeap);
pushMinHeap(maxHeap, nums[i]);
}
}
// Return the elements in the heap
return getMinHeap(maxHeap);
}
/* Push an element onto the heap */
function pushMinHeap(maxHeap: MaxHeap, val: number): void {
// Negate the value
maxHeap.push(-val);
}
/* Pop an element from the heap */
function popMinHeap(maxHeap: MaxHeap): number {
// Negate the value
return -maxHeap.pop();
}
/* Access the top element of the heap */
function peekMinHeap(maxHeap: MaxHeap): number {
// Negate the value
return -maxHeap.peek();
}
/* Get the elements in the heap */
function getMinHeap(maxHeap: MaxHeap): number[] {
// Negate the value
return maxHeap.getMaxHeap().map((num: number) => -num);
}
/* Use a heap to find the k largest elements in an array */
function topKHeap(nums: number[], k: number): number[] {
// Initialize a min heap
// Note: negate all values in the heap to simulate a min heap with a max heap
const maxHeap = new MaxHeap([]);
// Push the first k array elements onto the heap
for (let i = 0; i < k; i++) {
pushMinHeap(maxHeap, nums[i]);
}
// Starting with element k+1, keep the heap size at k
for (let i = k; i < nums.length; i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > peekMinHeap(maxHeap)) {
popMinHeap(maxHeap);
pushMinHeap(maxHeap, nums[i]);
}
}
// Return the elements in the heap
return getMinHeap(maxHeap);
}
/* Use a heap to find the k largest elements in an array */
MinHeap topKHeap(List<int> nums, int k) {
// Initialize a min heap with the first k elements of the array
MinHeap heap = MinHeap(nums.sublist(0, k));
// Starting with element k+1, keep the heap size at k
for (int i = k; i < nums.length; i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > heap.peek()) {
heap.pop();
heap.push(nums[i]);
}
}
return heap;
}
/* Use a heap to find the k largest elements in an array */
fn top_k_heap(nums: Vec<i32>, k: usize) -> BinaryHeap<Reverse<i32>> {
// BinaryHeap is a max heap; Reverse reverses the comparison order to implement a min heap
let mut heap = BinaryHeap::<Reverse<i32>>::new();
// Push the first k array elements onto the heap
for &num in nums.iter().take(k) {
heap.push(Reverse(num));
}
// Starting with element k+1, keep the heap size at k
for &num in nums.iter().skip(k) {
// If the current element is larger than the heap top, pop the top element and push the current element
if num > heap.peek().unwrap().0 {
heap.pop();
heap.push(Reverse(num));
}
}
heap
}
/* Push an element onto the heap */
void pushMinHeap(MaxHeap *maxHeap, int val) {
// Negate the element
push(maxHeap, -val);
}
/* Pop an element from the heap */
int popMinHeap(MaxHeap *maxHeap) {
// Negate the element
return -pop(maxHeap);
}
/* Access the top element of the heap */
int peekMinHeap(MaxHeap *maxHeap) {
// Negate the element
return -peek(maxHeap);
}
/* Get the elements in the heap */
int *getMinHeap(MaxHeap *maxHeap) {
// Negate every element in the heap and store the results in array res
int *res = (int *)malloc(maxHeap->size * sizeof(int));
for (int i = 0; i < maxHeap->size; i++) {
res[i] = -maxHeap->data[i];
}
return res;
}
/* Get the elements in the heap */
int *getMinHeap(MaxHeap *maxHeap) {
// Negate every element in the heap and store the results in array res
int *res = (int *)malloc(maxHeap->size * sizeof(int));
for (int i = 0; i < maxHeap->size; i++) {
res[i] = -maxHeap->data[i];
}
return res;
}
// Use a heap to find the k largest elements in an array
int *topKHeap(int *nums, int sizeNums, int k) {
// Initialize a min heap
// Note: negate every element to simulate a min heap with a max heap
int *empty = (int *)malloc(0);
MaxHeap *maxHeap = newMaxHeap(empty, 0);
// Push the first k array elements onto the heap
for (int i = 0; i < k; i++) {
pushMinHeap(maxHeap, nums[i]);
}
// Starting with element k+1, keep the heap size at k
for (int i = k; i < sizeNums; i++) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > peekMinHeap(maxHeap)) {
popMinHeap(maxHeap);
pushMinHeap(maxHeap, nums[i]);
}
}
int *res = getMinHeap(maxHeap);
// Free memory
delMaxHeap(maxHeap);
return res;
}
/* Use a heap to find the k largest elements in an array */
fun topKHeap(nums: IntArray, k: Int): Queue<Int> {
// Initialize a min heap
val heap = PriorityQueue<Int>()
// Push the first k array elements onto the heap
for (i in 0..<k) {
heap.offer(nums[i])
}
// Starting with element k+1, keep the heap size at k
for (i in k..<nums.size) {
// If the current element is larger than the heap top, pop the top element and push the current element
if (nums[i] > heap.peek()) {
heap.poll()
heap.offer(nums[i])
}
}
return heap
}
### Use a heap to find the k largest elements in an array ###
def top_k_heap(nums, k)
# Initialize a min heap
# Note: negate all values in the heap to simulate a min heap with a max heap
max_heap = MaxHeap.new([])
# Push the first k array elements onto the heap
for i in 0...k
push_min_heap(max_heap, nums[i])
end
# Starting with element k+1, keep the heap size at k
for i in k...nums.length
# If the current element is larger than the heap top, pop the top element and push the current element
if nums[i] > peek_min_heap(max_heap)
pop_min_heap(max_heap)
push_min_heap(max_heap, nums[i])
end
end
get_min_heap(max_heap)
end
Visualize execution
There are \(n\) rounds of heap insertion and removal to consider, and the heap has at most \(k\) elements, so the time complexity is \(O(n \log k)\). This is highly efficient: when \(k\) is small, the complexity approaches \(O(n)\); when \(k\) is large, it does not exceed \(O(n \log n)\).
This approach also suits dynamic data streams. As new data arrives, we can maintain the heap to update the largest \(k\) elements continuously.