2244. Minimum Rounds to Complete All Tasks LeetCode Solution

In this guide, you will get 2244. Minimum Rounds to Complete All Tasks LeetCode Solution with the best time and space complexity. The solution to Minimum Rounds to Complete All Tasks problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Minimum Rounds to Complete All Tasks solution in C++
  4. Minimum Rounds to Complete All Tasks solution in Java
  5. Minimum Rounds to Complete All Tasks solution in Python
  6. Additional Resources
2244. Minimum Rounds to Complete All Tasks LeetCode Solution image

Problem Statement of 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.

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.

See also  182. Duplicate Emails LeetCode Solution

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

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

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(n)

2244. Minimum Rounds to Complete All Tasks LeetCode Solution in C++

class Solution {
 public:
  int minimumRounds(vector<int>& tasks) {
    int ans = 0;
    unordered_map<int, int> count;

    for (const int task : tasks)
      ++count[task];

    // freq = 1 -> it's impossible
    // freq = 2 -> needs 1 round
    // freq = 3 -> needs 1 round
    // freq = 3k                           -> needs k rounds
    // freq = 3k + 1 = 3 * (k - 1) + 2 * 2 -> needs k + 1 rounds
    // freq = 3k + 2 = 3 * k       + 2 * 1 -> needs k + 1 rounds
    for (const auto& [_, freq] : count)
      if (freq == 1)
        return -1;
      else
        ans += (freq + 2) / 3;

    return ans;
  }
};
/* code provided by PROGIEZ */

2244. Minimum Rounds to Complete All Tasks LeetCode Solution in Java

class Solution {
  public int minimumRounds(int[] tasks) {
    int ans = 0;
    Map<Integer, Integer> count = new HashMap<>();

    for (final int task : tasks)
      count.merge(task, 1, Integer::sum);

    // freq = 1 -> it's impossible
    // freq = 2 -> needs 1 round
    // freq = 3 -> needs 1 round
    // freq = 3k                           -> needs k rounds
    // freq = 3k + 1 = 3 * (k - 1) + 2 * 2 -> needs k + 1 rounds
    // freq = 3k + 2 = 3 * k       + 2 * 1 -> needs k + 1 rounds
    for (final int freq : count.values())
      if (freq == 1)
        return -1;
      else
        ans += (freq + 2) / 3;

    return ans;
  }
}
// code provided by PROGIEZ

2244. Minimum Rounds to Complete All Tasks LeetCode Solution in Python

class Solution:
  def minimumRounds(self, tasks: list[int]) -> int:
    freqs = collections.Counter(tasks).values()
    return -1 if 1 in freqs else sum((f + 2) // 3 for f in freqs)
# code by PROGIEZ

Additional Resources

See also  1535. Find the Winner of an Array Game LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.