Top K Elements: Introduction
Let’s go over the Top K Elements pattern, its real-world applications, and some problems we can solve with it.
We'll cover the following
Overview#
The top K elements pattern helps find some specific number of elements from the given data with optimum time complexity.
Many problems ask us to find the top, the smallest, or the most/least frequent elements in an unsorted list of elements. To solve such problems, sorting the list takes time, then finding the elements takes time. However, the top elements pattern can allow us to solve the problem using time without sorting the list first.
Which data structure can we use to solve such problems? The best data structure to keep track of the smallest or largest elements is heap. With this pattern, we either use a max-heap or a min-heap to find the smallest or largest elements, respectively.
For example, let’s look at how this pattern takes steps to solve the problem of finding the top largest elements (using min-heap) or top smallest elements (using max-heap):
-
Insert the first elements from the given set of elements to the min-heap or max-heap.
-
Iterate through the rest of the elements.
- For min-heap, if you find the larger element, remove the top (smallest number) of the min-heap and insert the new larger element.
- For max-heap, if you find the smaller element, remove the top (largest number) of the max-heap and insert the new smaller element.
Iterating the complete list takes time, and the heap takes time for insertion. However, we get the access to the elements using the heap.
Let’s look at the following illustration to understand how we can use min-heap to find the top elements.
1 of 5
2 of 5
3 of 5
4 of 5
5 of 5
Examples#
The following examples illustrate some problems that can be solved with this approach:
1 of 2
2 of 2
Does my problem match this pattern?#
-
Yes, if both of these conditions are fulfilled:
-
We need to find the largest, smallest, most frequent, or least frequent subset of elements in an unsorted list.
-
This may be the requirement of the final solution, or it may be necessary as an intermediate step toward the final solution.
-
-
No, if any of these conditions is fulfilled:
- The input data structure does not support random access.
- The input data is already sorted according to the criteria relevant to solving the problem.
- If only extreme value is required, that is, , as that problem can be solved in with a simple scan through the input array.
Real-world problems#
Many problems in the real world use the top K elements pattern. Let’s look at some examples.
-
Uber: Select at least the nearest drivers within the user’s vicinity, avoiding the drivers that are too far away.
-
Stocks: Given the set of IDs of brokers, determine the top K broker’s performance with the frequently repeated IDs in the given data set.
Strategy time!#
Match the problems that can be solved using the top K elements pattern.
Note: Select a problem in the left-hand column by clicking it, and then click one of the two options in the right-hand column.
Rearrange the given string so that no two identical characters are adjacent to each other.
Top K Elements
Detect a cycle in a linked list.
Some other pattern
Find the five nearest points to the origin.
Implement a stack in which the pop operation removes the most frequent element.
Solution: Find All Anagrams in a String
K Closest Points to Origin