Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.
Example 1:
Input: arr = [5,5,4], k = 1 Output: 1 Explanation: Remove the single 4, only 5 is left.Example 2:
Input: arr = [4,3,1,1,3,3,2], k = 3 Output: 2 Explanation: Remove 4, 2 and either one of the two 1s or three 3s. 1 and 3 will be left.
Constraints:
1 <= arr.length <= 10^51 <= arr[i] <= 10^90 <= k <= arr.lengthWhen you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:
Imagine you have a collection of different numbered items and you are allowed to throw away a specific number of them. The brute force approach involves trying every single possible combination of items to throw away, and for each choice, counting how many different types of numbers are left over.
Here's how the algorithm would work step-by-step:
import itertools
def find_least_unique_integers(arr, removals_allowed):
# We generate all combinations of elements to remove in order to explore every possible scenario.
all_possible_removals = list(itertools.combinations(arr, removals_allowed))
minimum_unique_count = len(set(arr))
for removal_combination in all_possible_removals:
remaining_elements = list(arr)
for item_to_remove in removal_combination:
remaining_elements.remove(item_to_remove)
# Evaluating the unique count for each combination allows us to track the absolute minimum left over.
current_unique_count = len(set(remaining_elements))
if current_unique_count < minimum_unique_count:
minimum_unique_count = current_unique_count
return minimum_unique_countTo find the fewest number of unique integers after removing a certain number of elements, we should get rid of the numbers that appear least often first. By targeting the rarest items entirely, we make the most efficient use of our removals and eliminate as many distinct values as possible.
Here's how the algorithm would work step-by-step:
from collections import Counter
def find_least_num_of_unique_ints(arr, removals):
# Counting frequencies allows us to identify and target the rarest integers first.
frequency_counts = Counter(arr)
sorted_frequencies = sorted(frequency_counts.values())
unique_integer_count = len(sorted_frequencies)
for frequency in sorted_frequencies:
# We stop when remaining removals cannot fully eliminate the next rarest number group.
if removals >= frequency:
removals -= frequency
unique_integer_count -= 1
else:
break
return unique_integer_count| Case | How to Handle |
|---|---|
| K is greater than or equal to the total number of elements in the array | The greedy removal of lowest frequencies will exhaust all elements, resulting in zero unique integers left. |
| K is zero, meaning no elements are removed | The algorithm skips removal entirely and simply returns the initial count of unique integers in the frequency map. |
| All elements in the array are identical | The frequency map will contain a single entry, and subtracting K from its count will either leave 1 unique integer or 0 if K is greater than or equal to the array length. |
| All elements in the array are unique | Every element has a frequency of one, so removing K elements will simply reduce the number of unique integers by K. |
| Array contains negative numbers, zeros, and positive numbers | Using a hash map for frequency counting naturally accommodates any integer values as keys without requiring normalization. |
| Multiple elements share the exact same frequency | Sorting the frequencies ensures we greedily eliminate whichever elements have the smallest counts first, regardless of their actual value. |
| K is large enough to partially remove elements of a specific frequency group | The greedy loop checks if remaining K is less than the current frequency and breaks out early since we cannot fully remove that group. |
| Extremely large inputs reaching maximum constraint limits | Using an efficient O(N log N) sorting approach on unique frequencies ensures the solution executes well within standard time limits. |