Minimum Rounds to Complete All Tasks

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.

Note: This question is the same as 2870: Minimum Number of Operations to Make Array Empty.

Example 1
Inputtasks = [2,2,3,3,2,4,4,4,4,4]
Output4
A possible optimal plan uses one round for three tasks of difficulty 2, one round for two tasks of difficulty 3, and two rounds for all five tasks of difficulty 4, for a total of 4 rounds.
Example 2
Inputtasks = [2,3,3]
Output-1
There is only 1 task of difficulty level 2, but each round must complete either 2 or 3 tasks of the same difficulty level, so all tasks cannot be completed.

Constraints

  • 1 <= tasks.length <= 10^5
  • 1 <= tasks[i] <= 10^9

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate