LeetCode 1481: Least Number of Unique Integers after K Removals Solution

Master LeetCode problem 1481 (Least Number of Unique Integers after K Removals), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

1481. Least Number of Unique Integers after K Removals

Problem Explanation

Explanation

To solve this problem, we can follow these steps:

  1. Count the frequency of each integer in the array.
  2. Sort the integers based on their frequencies.
  3. Remove the least frequent integers one by one until we have removed k elements.
  4. Return the count of unique integers remaining after removal.

Time complexity: O(n log n) where n is the number of integers in the array. Space complexity: O(n) to store the frequency of each integer.

Solution Code

import java.util.*;

class Solution {
    public int findLeastNumOfUniqueInts(int[] arr, int k) {
        Map<Integer, Integer> freqMap = new HashMap<>();
        for (int num : arr) {
            freqMap.put(num, freqMap.getOrDefault(num, 0) + 1);
        }
        
        List<Integer> freqList = new ArrayList<>(freqMap.values());
        Collections.sort(freqList);
        
        int uniqueCount = freqList.size();
        int count = 0;
        for (int freq : freqList) {
            if (k >= freq) {
                k -= freq;
                count++;
            } else {
                break;
            }
        }
        
        return uniqueCount - count;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 1481 (Least Number of Unique Integers after K Removals)?

This page provides optimized solutions for LeetCode problem 1481 (Least Number of Unique Integers after K Removals) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 1481 (Least Number of Unique Integers after K Removals)?

The time complexity for LeetCode 1481 (Least Number of Unique Integers after K Removals) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 1481 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 1481 in Java, C++, or Python.

Back to LeetCode Solutions