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.
You are given a 0-indexed array nums consisting of positive integers.
There are two types of operations that you can apply on the array any number of times:
Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
Example 1:
Input: nums = [2,3,3,2,2,4,2,3,4] Output: 4 Explanation: We can apply the following operations to make the array empty: - Apply the first operation on the elements at indices 0 and 3. The resulting array is nums = [3,3,2,4,2,3,4]. - Apply the first operation on the elements at indices 2 and 4. The resulting array is nums = [3,3,4,3,4]. - Apply the second operation on the elements at indices 0, 1, and 3. The resulting array is nums = [4,4]. - Apply the first operation on the elements at indices 0 and 1. The resulting array is nums = []. It can be shown that we cannot make the array empty in less than 4 operations.
Example 2:
Input: nums = [2,1,2,2,3,3] Output: -1 Explanation: It is impossible to empty the array.
Constraints:
2 <= nums.length <= 1051 <= nums[i] <= 106Note: This question is the same as 2244: Minimum Rounds to Complete All Tasks.
Problem summary: You are given a 0-indexed array nums consisting of positive integers. There are two types of operations that you can apply on the array any number of times: Choose two elements with equal values and delete them from the array. Choose three elements with equal values and delete them from the array. Return the minimum number of operations required to make the array empty, or -1 if it is not possible.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[2,3,3,2,2,4,2,3,4]
[2,1,2,2,3,3]
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2870: Minimum Number of Operations to Make Array Empty
class Solution {
public int minOperations(int[] nums) {
Map<Integer, Integer> count = new HashMap<>();
for (int num : nums) {
// count.put(num, count.getOrDefault(num, 0) + 1);
count.merge(num, 1, Integer::sum);
}
int ans = 0;
for (int c : count.values()) {
if (c < 2) {
return -1;
}
int r = c % 3;
int d = c / 3;
switch (r) {
case (0) -> {
ans += d;
}
default -> {
ans += d + 1;
}
}
}
return ans;
}
}
// Accepted solution for LeetCode #2870: Minimum Number of Operations to Make Array Empty
func minOperations(nums []int) (ans int) {
count := map[int]int{}
for _, num := range nums {
count[num]++
}
for _, c := range count {
if c < 2 {
return -1
}
ans += (c + 2) / 3
}
return
}
# Accepted solution for LeetCode #2870: Minimum Number of Operations to Make Array Empty
class Solution:
def minOperations(self, nums: List[int]) -> int:
count = Counter(nums)
ans = 0
for c in count.values():
if c == 1:
return -1
ans += (c + 2) // 3
return ans
// Accepted solution for LeetCode #2870: Minimum Number of Operations to Make Array Empty
// Rust example auto-generated from java reference.
// Replace the signature and local types with the exact LeetCode harness for this problem.
impl Solution {
pub fn rust_example() {
// Port the logic from the reference block below.
}
}
// Reference (java):
// // Accepted solution for LeetCode #2870: Minimum Number of Operations to Make Array Empty
// class Solution {
// public int minOperations(int[] nums) {
// Map<Integer, Integer> count = new HashMap<>();
// for (int num : nums) {
// // count.put(num, count.getOrDefault(num, 0) + 1);
// count.merge(num, 1, Integer::sum);
// }
// int ans = 0;
// for (int c : count.values()) {
// if (c < 2) {
// return -1;
// }
// int r = c % 3;
// int d = c / 3;
// switch (r) {
// case (0) -> {
// ans += d;
// }
// default -> {
// ans += d + 1;
// }
// }
// }
// return ans;
// }
// }
// Accepted solution for LeetCode #2870: Minimum Number of Operations to Make Array Empty
function minOperations(nums: number[]): number {
const count: Map<number, number> = new Map();
for (const num of nums) {
count.set(num, (count.get(num) ?? 0) + 1);
}
let ans = 0;
for (const [_, c] of count) {
if (c < 2) {
return -1;
}
ans += ((c + 2) / 3) | 0;
}
return ans;
}
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.