Advertisement

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

Least Number of Unique Integers after K Removals - Complete Solution Guide

Least Number of Unique Integers after K Removals is LeetCode problem 1481, a Medium level challenge. This complete guide provides step-by-step explanations, multiple solution approaches, and optimized code in python3, java, cpp, c.

Problem Statement

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^5 1 <= arr[i] <= 10^9 0 <= k <= arr.length

Detailed Explanation

The problem asks us to find the least number of unique integers remaining in an array after removing exactly `k` elements. We are given an array `arr` of integers and an integer `k`. The goal is to minimize the number of distinct elements left in the array after deleting `k` elements in total. We must remove *exactly* `k` elements, no more and no less.

Solution Approach

The solution uses a greedy approach. First, it counts the frequency of each element in the input array using a hash table. Then, it sorts these frequencies in ascending order. Finally, it iterates through the sorted frequencies, removing elements from the array by decrementing `k` by the frequency of each number as long as `k` is greater than or equal to the current frequency. If `k` becomes less than the current frequency, it stops removing elements. The remaining number of frequencies is the least number of unique integers left in the array.

Step-by-Step Algorithm

  1. Step 1: Count the frequency of each element in the input array `arr`. Use a hash table (dictionary) to store the element as a key and its frequency as the value.
  2. Step 2: Extract the frequencies from the hash table and store them in a list (or array).
  3. Step 3: Sort the list of frequencies in ascending order.
  4. Step 4: Iterate through the sorted frequencies. For each frequency, check if `k` is greater than or equal to the frequency. If it is, subtract the frequency from `k` and decrement the number of unique integers (initialized to the size of the frequency list).
  5. Step 5: If `k` becomes less than the current frequency, stop the iteration because we cannot completely remove this element and hence, it will remain unique. Return the remaining number of unique integers.

Key Insights

  • Insight 1: The key is to remove elements based on their frequency in the array. Removing elements with lower frequencies first is a greedy approach that minimizes the number of unique integers.
  • Insight 2: Using a hash table (or dictionary) to count the occurrences of each number is efficient for determining the frequency of each element.
  • Insight 3: Sorting the frequencies allows us to strategically remove elements from the array, prioritizing those that appear least frequently.

Complexity Analysis

Time Complexity: O(n log n)

Space Complexity: O(n)

Topics

This problem involves: Array, Hash Table, Greedy, Sorting, Counting.

Companies

Asked at: Expedia, IBM, J.P. Morgan.