Minimum Rounds to Complete All Tasks (LeetCode 2244): Solution and Explanation

Medium
Array
Hash table
Greedy
Counting

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)

  1. Count how many tasks each difficulty level has.
  2. For each count c: if c is 1, return -1.
  3. Otherwise add (c + 2) // 3 to the running total.
  4. Return the total.

Step-by-step example

tasks = [2,2,3,3,2,4,4,4,4,4]
  1. Counts: level 2 has 3 tasks, level 3 has 2, level 4 has 5.
  2. Level 2: one round of 3. Total 1.
  3. Level 3: one round of 2. Total 2.
  4. Level 4: 5 = 3 + 2, two rounds. Total 4.
  5. 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 rounds
Press play on the visualizer and the running line lights up here.

Time and space complexity

Work as the input growsinput size n →steps
Count per level, then ceil(c / 3) O(n)
DP over counts O(n)
Sort and scan runs O(n log n)
Time

O(n)

One pass to count, then one pass over the distinct levels.

Space

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.