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 integer array tasks, where tasks[i] represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level.
Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks.
Example 1:
Input: tasks = [2,2,3,3,2,4,4,4,4,4] Output: 4 Explanation: To complete all the tasks, a possible plan is: - In the first round, you complete 3 tasks of difficulty level 2. - In the second round, you complete 2 tasks of difficulty level 3. - In the third round, you complete 3 tasks of difficulty level 4. - In the fourth round, you complete 2 tasks of difficulty level 4. It can be shown that all the tasks cannot be completed in fewer than 4 rounds, so the answer is 4.
Example 2:
Input: tasks = [2,3,3] Output: -1 Explanation: There is only 1 task of difficulty level 2, but in each round, you can only complete either 2 or 3 tasks of the same difficulty level. Hence, you cannot complete all the tasks, and the answer is -1.
Constraints:
1 <= tasks.length <= 1051 <= tasks[i] <= 109Note: This question is the same as 2870: Minimum Number of Operations to Make Array Empty.
Problem summary: You are given a 0-indexed integer array tasks, where tasks[i] represents the difficulty level of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level. Return the minimum rounds required to complete all the tasks, or -1 if it is not possible to complete all the tasks.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Array · Hash Map · Greedy
[2,2,3,3,2,4,4,4,4,4]
[2,3,3]
climbing-stairs)odd-string-difference)minimum-levels-to-gain-more-points)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2244: Minimum Rounds to Complete All Tasks
class Solution {
public int minimumRounds(int[] tasks) {
Map<Integer, Integer> cnt = new HashMap<>();
for (int t : tasks) {
cnt.merge(t, 1, Integer::sum);
}
int ans = 0;
for (int v : cnt.values()) {
if (v == 1) {
return -1;
}
ans += v / 3 + (v % 3 == 0 ? 0 : 1);
}
return ans;
}
}
// Accepted solution for LeetCode #2244: Minimum Rounds to Complete All Tasks
func minimumRounds(tasks []int) int {
cnt := map[int]int{}
for _, t := range tasks {
cnt[t]++
}
ans := 0
for _, v := range cnt {
if v == 1 {
return -1
}
ans += v / 3
if v%3 != 0 {
ans++
}
}
return ans
}
# Accepted solution for LeetCode #2244: Minimum Rounds to Complete All Tasks
class Solution:
def minimumRounds(self, tasks: List[int]) -> int:
cnt = Counter(tasks)
ans = 0
for v in cnt.values():
if v == 1:
return -1
ans += v // 3 + (v % 3 != 0)
return ans
// Accepted solution for LeetCode #2244: Minimum Rounds to Complete All Tasks
use std::collections::HashMap;
impl Solution {
pub fn minimum_rounds(tasks: Vec<i32>) -> i32 {
let mut cnt = HashMap::new();
for &t in tasks.iter() {
let count = cnt.entry(t).or_insert(0);
*count += 1;
}
let mut ans = 0;
for &v in cnt.values() {
if v == 1 {
return -1;
}
ans += v / 3 + (if v % 3 == 0 { 0 } else { 1 });
}
ans
}
}
// Accepted solution for LeetCode #2244: Minimum Rounds to Complete All Tasks
function minimumRounds(tasks: number[]): number {
const cnt = new Map();
for (const t of tasks) {
cnt.set(t, (cnt.get(t) || 0) + 1);
}
let ans = 0;
for (const v of cnt.values()) {
if (v == 1) {
return -1;
}
ans += Math.floor(v / 3) + (v % 3 === 0 ? 0 : 1);
}
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.