Minimum Rounds to Complete All Tasks (LeetCode 2244): Solution and Explanation
Each round finishes 2 or 3 tasks that share a difficulty level. Given tasks, return the fewest rounds that finish everything, or -1 if some task can never be placed in a round.
Intuition
A round never mixes levels, so every level is its own small puzzle: how few groups of 2 and 3 add up to c, the number of tasks at that level?
If c is 1, no round can take that task, and the whole answer is -1.
Otherwise use rounds of 3 as much as possible. A leftover of 2 is one extra round. A leftover of 1 is fixed by breaking one round of 3 into two rounds of 2, which still costs only one extra round.
In every case with c >= 2 the count is ceil(c / 3), which integer math writes as (c + 2) // 3.
Approach: Count per level, then ceil(c / 3)
- Count how many tasks each difficulty level has.
- For each count c: if c is 1, return -1.
- Otherwise add
(c + 2) // 3to the running total. - Return the total.
Step-by-step example
tasks = [2,2,3,3,2,4,4,4,4,4]
- Counts: level 2 has 3 tasks, level 3 has 2, level 4 has 5.
- Level 2: one round of 3. Total 1.
- Level 3: one round of 2. Total 2.
- Level 4: 5 = 3 + 2, two rounds. Total 4.
- No level has a single task, so the answer is 4.
Code: Python, Java, C++
1class Solution:
2 def minimumRounds(self, tasks: List[int]) -> int:
3 count = Counter(tasks)
4 rounds = 0
5 for c in count.values():
6 if c == 1:
7 return -1
8 rounds += (c + 2) // 3
9 return rounds1class Solution {
2 public int minimumRounds(int[] tasks) {
3 Map<Integer, Integer> count = new HashMap<>();
4 for (int t : tasks) {
5 count.put(t, count.getOrDefault(t, 0) + 1);
6 }
7 int rounds = 0;
8 for (int c : count.values()) {
9 if (c == 1) {
10 return -1;
11 }
12 rounds += (c + 2) / 3;
13 }
14 return rounds;
15 }
16}1class Solution {
2public:
3 int minimumRounds(vector<int>& tasks) {
4 unordered_map<int, int> count;
5 for (int t : tasks) {
6 count[t]++;
7 }
8 int rounds = 0;
9 for (auto& [task, c] : count) {
10 if (c == 1) {
11 return -1;
12 }
13 rounds += (c + 2) / 3;
14 }
15 return rounds;
16 }
17};Time and space complexity
O(n)
One pass to count, then one pass over the distinct levels.
O(n)
The hash map can hold up to n / 2 distinct levels.
Other approaches
Sort and scan runs (time O(n log n), space O(1))
Sort the array, walk each run of equal values and apply the same ceil(c / 3) rule. No hash map, but sorting costs more.
DP over counts (time O(n), space O(n))
Compute the fewest rounds for every count with dp[c] = 1 + min(dp[c - 2], dp[c - 3]). Correct, but the closed form makes it unnecessary.
Common mistakes
- Using c // 3 + c % 3, which overcounts when the leftover is 2 (it adds 2 rounds instead of 1).
- Treating a leftover of 1 as impossible, when 4 = 2 + 2 and 7 = 3 + 2 + 2 both work.
- Forgetting the -1 case for a level that appears exactly once.
- Mixing tasks of different levels in one round.
Frequently asked questions
Why is (c + 2) // 3 always achievable?
Write c = 3q + r. If r is 0 you use q rounds. If r is 2 you add one round of 2. If r is 1 and c >= 4, swap one 3 for two 2s, giving q + 1 rounds. Each case equals ceil(c / 3).
Why can it not be done in fewer rounds?
Each round finishes at most 3 tasks, so c tasks need at least ceil(c / 3) rounds. The greedy split reaches that bound.
When is the answer -1?
Exactly when some level has a single task. Every count of 2 or more can be written as a sum of 2s and 3s.