Leetcode Problem 1481. Least Number of Unique Integers after K Removals

1481. Least Number of Unique Integers after K Removals

Leetcode Solutions

Greedy Approach with Frequency Sorting

  1. Create a frequency map to count the occurrences of each unique integer in the array.
  2. Convert the frequency map into a list of pairs (or a list of lists), where each pair contains the unique integer and its frequency.
  3. Sort the list of pairs based on the frequency in ascending order.
  4. Initialize a variable remaining to the size of the frequency map, which represents the number of unique integers.
  5. Iterate over the sorted list of pairs:
    • If k is greater than or equal to the frequency of the current pair, subtract the frequency from k and decrement remaining by 1.
    • If k is less than the frequency, break the loop as we cannot remove more integers.
  6. Return the value of remaining, which is the least number of unique integers after removing k elements.
UML Thumbnail

HashMap and PriorityQueue Approach

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...