LeetCode Problem

How to Solve Minimum Rounds to Complete All Tasks

This problem asks you to complete tasks in the minimum number of rounds, where each round allows you to complete either 2 or 3 tasks of the same difficulty. The solution involves counting the frequency of each difficulty level and then applying a greedy approach to group the tasks into the fewest rounds possible. If any difficulty level has fewer than two tasks, it's impossible to complete all tasks, and the answer should be -1.

GhostInterview Help

Need help with Minimum Rounds to Complete All Tasks without spending extra time grinding it?

GhostInterview can read Minimum Rounds to Complete All Tasks from a screenshot, generate the answer path, explain the complexity, and support solver-first interview workflows when you need direct help fast.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.

Problem #2244Array scanning plus hash lookupReviewed 2026-03-08
Difficulty
Medium
Primary pattern
Array scanning plus hash lookup
Answer-first problem summary
Step-by-step approach and complexity
GhostInterview solver workflow

This problem asks you to complete tasks in the minimum number of rounds, where each round allows you to complete either 2 or 3 tasks of the same difficulty. The solution involves counting the frequency of each difficulty level and then applying a greedy approach to group the tasks into the fewest rounds possible. If any difficulty level has fewer than two tasks, it's impossible to complete all tasks, and the answer should be -1.

Problem Statement

You are given an array of integers tasks, where each value represents the difficulty of a task. In each round, you can complete either 2 or 3 tasks of the same difficulty level. Your goal is to find the minimum number of rounds required to complete all tasks.

Return the minimum number of rounds, or -1 if it's impossible to complete the tasks due to insufficient tasks of certain difficulty levels. The key challenge is efficiently determining the minimum rounds while handling various difficulty levels using counting and greedy techniques.

Examples

Example 1

Input: tasks = [2,2,3,3,2,4,4,4,4,4]

Output: 4

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

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 <= 105
  • 1 <= tasks[i] <= 109

Solution Approach

Counting Task Frequencies

Start by counting the occurrences of each difficulty level using a hash table. This allows us to efficiently track how many tasks of each difficulty we need to group together into rounds.

Greedy Grouping

For each difficulty, apply a greedy approach to group tasks into as few rounds as possible. If there are n tasks of a certain difficulty, the minimum number of rounds required is ceil(n / 3). If any difficulty has fewer than 2 tasks, return -1 immediately.

Edge Case Handling

Consider edge cases such as having only one task of a particular difficulty level, which would make completing all tasks impossible. These cases should be caught early to optimize performance.

Complexity Analysis

MetricValue
TimeDepends on the final approach
SpaceDepends on the final approach

The time complexity depends on iterating over the array to count the frequencies, which is O(n), where n is the number of tasks. Then, for each unique difficulty level, we perform constant-time operations, leading to an overall time complexity of O(n). The space complexity is O(k), where k is the number of unique difficulty levels in the tasks array, as we store the frequency counts in a hash table.

What Interviewers Usually Probe

  • Evaluating the candidate's ability to use a hash table for counting frequencies and applying a greedy strategy.
  • Assessing how efficiently the candidate handles edge cases like insufficient tasks for a round.
  • Testing the candidate's ability to optimize for large input sizes by minimizing both time and space complexity.

Common Pitfalls or Variants

Common pitfalls

  • Failing to handle edge cases where a task count is less than 2 for a particular difficulty level.
  • Overcomplicating the solution by using unnecessary data structures or algorithms when a hash table and greedy approach are sufficient.
  • Not properly optimizing the solution to handle the upper constraint of 10^5 tasks efficiently.

Follow-up variants

  • Allowing multiple rounds of varying task counts (2, 3, or other counts).
  • Implementing the problem with additional constraints, such as requiring a specific task order.
  • Handling more complex variations with additional task dependencies or restrictions.

How GhostInterview Helps

  • GhostInterview's solver provides a structured approach, focusing on counting frequencies and applying a greedy solution to minimize rounds.
  • The tool highlights key performance optimizations, ensuring that your solution handles large inputs efficiently.
  • By pointing out common pitfalls, GhostInterview ensures your solution correctly accounts for edge cases like insufficient task counts.

Topic Pages

FAQ

What is the minimum rounds to complete all tasks problem?

This problem involves determining the fewest rounds required to complete all tasks, where each round can contain either 2 or 3 tasks of the same difficulty level.

How do you solve the minimum rounds problem?

You solve it by counting the frequency of each task difficulty using a hash table and then grouping tasks into rounds using a greedy approach.

What is the greedy approach used in this problem?

The greedy approach groups tasks into the fewest rounds by completing 3 tasks at a time whenever possible. If fewer than 3 tasks are left, complete 2 tasks in a round.

Why can't we complete tasks with only 1 of a difficulty level?

In this problem, you can only complete 2 or 3 tasks per round. Having only 1 task of a particular difficulty means it's impossible to complete that task, so the answer is -1.

What data structure is used to count task frequencies?

A hash table (or dictionary) is used to count the number of tasks for each difficulty level, allowing for efficient access and grouping.

GhostInterview Solver

Need direct help with Minimum Rounds to Complete All Tasks instead of spending more time grinding it?

Download GhostInterview when you want a LeetCode solver, not another long practice loop. Capture Minimum Rounds to Complete All Tasks from a screenshot, get the answer path and complexity, and use supported stealth workflows that stay outside captured layers.

Screenshot Input

Capture the prompt fast instead of rewriting the problem by hand.

Answer + Complexity

Get the solution path, trade-offs, and complexity summary in one pass.

Stealth Workflow

Stay outside captured layers on supported screen-share workflows.