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.
Example 1:
Input: tasks = [2,2,3,3,2,4,4,4,4,4] Output: 4 Explanation: 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 Explanation: 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 <= 1051 <= tasks[i] <= 109
Note: This question is the same as 2870: Minimum Number of Operations to Make Array Empty.
This approach involves breaking down the problem into smaller subproblems, solving each of them independently, and combining their results in an efficient way. It often uses recursion to handle each subproblem.
This C code uses a recursive function to implement the divide and conquer strategy. It divides the problem into two halves, solves each recursively, and then combines their solutions.
C++
Java
Python
C#
JavaScript
Time Complexity: T(n) = 2T(n/2) + O(n) => O(n log n)
Space Complexity: O(log n) due to recursion stack space.
This approach involves solving complex problems by breaking them into simpler overlapping subproblems, storing the results of subproblems to avoid redundant calculations, and constructing a solution from these stored results.
This C code uses dynamic programming to store results of subproblems. This avoids redundant computations, especially useful in fibonacci-style computations.
C++
Java
Python
C#
JavaScript
Time Complexity: O(n)
Space Complexity: O(n)
| Approach | Complexity |
|---|---|
| Divide and Conquer Approach | Time Complexity: T(n) = 2T(n/2) + O(n) => O(n log n) |
| Dynamic Programming Approach | Time Complexity: O(n) |
Minimum Rounds to Complete All Tasks | Leetcode 2244 | Maps Math | Contest 289 🔥🔥 • Coding Decoded • 2,494 views views
Watch 9 more video solutions →Practice Minimum Rounds to Complete All Tasks with our built-in code editor and test cases.
Practice on FleetCodePractice this problem
Open in Editor