Minimum Rounds to Complete All Tasks - Problem

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.

Input & Output

Example 1 — Mixed Difficulties
$ Input: tasks = [2,2,3,3,2,4,4,4,4,4]
Output: 4
💡 Note: Difficulty 2: 3 tasks → 1 round of 3. Difficulty 3: 2 tasks → 1 round of 2. Difficulty 4: 5 tasks → 1 round of 3 + 1 round of 2 = 2 rounds. Total: 1+1+2=4
Example 2 — Perfect Groups
$ Input: tasks = [2,3,3]
Output: -1
💡 Note: Difficulty 2: 1 task cannot be completed (need at least 2 tasks of same difficulty per round)
Example 3 — All Same Difficulty
$ Input: tasks = [5,5,5,5,5,5]
Output: 2
💡 Note: 6 tasks of difficulty 5 can be completed in 2 rounds of 3 tasks each

Constraints

  • 1 ≤ tasks.length ≤ 105
  • 1 ≤ tasks[i] ≤ 109

Visualization

Tap to expand
Minimum Rounds Problem: Group Tasks by DifficultyInput Array:2233244444Group by Difficulty:Difficulty 2Count: 3 → 1 roundDifficulty 3Count: 2 → 1 roundDifficulty 4Count: 5 → 2 roundsMinimum Rounds: 1 + 1 + 2 = 4
Understanding the Visualization
1
Input
Array of task difficulties: [2,2,3,3,2,4,4,4,4,4]
2
Group by Difficulty
Count frequencies: diff2→3, diff3→2, diff4→5
3
Minimize Rounds
Use math to find minimum rounds: 1+1+2=4 total
Key Takeaway
🎯 Key Insight: Count frequency of each difficulty, then use greedy math to minimize rounds
Asked in
Amazon 45 Google 30 Microsoft 25
28.0K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen