See the original problem on HackerRank.
Solutions
The brute force solution has a quadratic time complexity and involves iterating through each value while checking if it forms a matching pair.
For example:
|
|
A more optimized, linear, solution involves keeping track of the positions of the last occurrence of each card value. As we iterate through the array, we calculate the minimum number of picks needed to form a matching pair. The interesting part of this solution lies in the choice of data structures used to memorize the last positions of card values, as this directly impacts the efficiency and simplicity of the solution.
For example, we might use a map (either tree-based or hash-based), such as:
|
|
Or directly an array, since the domain is quite limited (the card values span from \(0\) to \(10^6\)):
|
|
Regardless of the data structure choice, the solution is online, meaning it processes the input one element at a time without needing to store the entire card deck in memory. The space complexity only depends on the domain size, thus is “constant” - specifically, it corresponds to the number of unique card values that can appear in the input (\(10^6 + 1\)).
Here is a simple benchmark showing performance differences of the three main data structures proposed. Feel free to experiment with different input sizes and variations to gain deeper insights into the performance of each of the three solutions.