Off-by-one on range boundaries
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Move from brute-force thinking to an efficient approach using array strategy.
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.lengthProblem summary: Given an array of integers arr and an integer k. Find the least number of unique integers after removing exactly k elements.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[5,5,4] 1
[4,3,1,1,3,3,2] 3
maximum-number-of-distinct-elements-after-operations)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #1481: Least Number of Unique Integers after K Removals
class Solution {
public int findLeastNumOfUniqueInts(int[] arr, int k) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int x : arr) {
cnt.merge(x, 1, Integer::sum);
}
List<Integer> nums = new ArrayList<>(cnt.values());
Collections.sort(nums);
for (int i = 0, m = nums.size(); i < m; ++i) {
k -= nums.get(i);
if (k < 0) {
return m - i;
}
}
return 0;
}
}
// Accepted solution for LeetCode #1481: Least Number of Unique Integers after K Removals
func findLeastNumOfUniqueInts(arr []int, k int) int {
cnt := map[int]int{}
for _, x := range arr {
cnt[x]++
}
nums := make([]int, 0, len(cnt))
for _, v := range cnt {
nums = append(nums, v)
}
sort.Ints(nums)
for i, v := range nums {
k -= v
if k < 0 {
return len(nums) - i
}
}
return 0
}
# Accepted solution for LeetCode #1481: Least Number of Unique Integers after K Removals
class Solution:
def findLeastNumOfUniqueInts(self, arr: List[int], k: int) -> int:
cnt = Counter(arr)
for i, v in enumerate(sorted(cnt.values())):
k -= v
if k < 0:
return len(cnt) - i
return 0
// Accepted solution for LeetCode #1481: Least Number of Unique Integers after K Removals
struct Solution;
use std::cmp::Reverse;
use std::collections::BinaryHeap;
use std::collections::HashMap;
impl Solution {
fn find_least_num_of_unique_ints(arr: Vec<i32>, k: i32) -> i32 {
let mut k = k as usize;
let mut count: HashMap<i32, usize> = HashMap::new();
for x in arr {
*count.entry(x).or_default() += 1;
}
let mut queue: BinaryHeap<Reverse<usize>> = BinaryHeap::new();
for (_, v) in count {
queue.push(Reverse(v));
}
while let Some(&min) = queue.peek() {
if min.0 <= k {
k -= min.0;
queue.pop();
} else {
break;
}
}
queue.len() as i32
}
}
#[test]
fn test() {
let arr = vec![5, 5, 4];
let k = 1;
let res = 1;
assert_eq!(Solution::find_least_num_of_unique_ints(arr, k), res);
let arr = vec![4, 3, 1, 1, 3, 3, 2];
let k = 3;
let res = 2;
assert_eq!(Solution::find_least_num_of_unique_ints(arr, k), res);
}
// Accepted solution for LeetCode #1481: Least Number of Unique Integers after K Removals
function findLeastNumOfUniqueInts(arr: number[], k: number): number {
const cnt: Map<number, number> = new Map();
for (const x of arr) {
cnt.set(x, (cnt.get(x) ?? 0) + 1);
}
const nums = [...cnt.values()].sort((a, b) => a - b);
for (let i = 0; i < nums.length; ++i) {
k -= nums[i];
if (k < 0) {
return nums.length - i;
}
}
return 0;
}
Use this to step through a reusable interview workflow for this problem.
Try every possible combination of choices. With n items each having two states (include/exclude), the search space is 2ⁿ. Evaluating each combination takes O(n), giving O(n × 2ⁿ). The recursion stack or subset storage uses O(n) space.
Greedy algorithms typically sort the input (O(n log n)) then make a single pass (O(n)). The sort dominates. If the input is already sorted or the greedy choice can be computed without sorting, time drops to O(n). Proving greedy correctness (exchange argument) is harder than the implementation.
Review these before coding to avoid predictable interview regressions.
Wrong move: Loop endpoints miss first/last candidate.
Usually fails on: Fails on minimal arrays and exact-boundary answers.
Fix: Re-derive loops from inclusive/exclusive ranges before coding.
Wrong move: Zero-count keys stay in map and break distinct/count constraints.
Usually fails on: Window/map size checks are consistently off by one.
Fix: Delete keys when count reaches zero.
Wrong move: Locally optimal choices may fail globally.
Usually fails on: Counterexamples appear on crafted input orderings.
Fix: Verify with exchange argument or monotonic objective before committing.