Minimum Rounds to Complete All Tasks — LeetCode 2244 Python Solution

MediumGreedyArrayHash TableCounting
Problem
#2244
Pattern
Greedy
Reading time
2 min

The 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.

Example

Input
tasks = [2,2,3,3,2,4,4,4,4,4]
Output
4
Explanation
To complete all the tasks, a possible plan is:

Python solution

Python
class Solution:
    def minimumRounds(self, tasks: List[int]) -> int:
        cnt = Counter(tasks)
        ans = 0
        for v in cnt.values():
            if v == 1:
                return -1
            ans += v // 3 + (v % 3 != 0)
        return ans

Complexity

MeasureComplexity
TimeO(n)
SpaceO(n), where n is the length of the `tasks` array auxiliary

Pattern: Greedy

Take the locally best option every time — when you can prove that never costs you later. LeetCode 2244. Minimum Rounds to Complete All Tasks is filed here because LeetCode tags it Greedy, which is the vocabulary this hub collects.

The greedy guide has the Python template for the pattern and the 346 LeetCode problems that use it.

Related problems

Frequently asked questions

How hard is LeetCode 2244. Minimum Rounds to Complete All Tasks?
LeetCode 2244. Minimum Rounds to Complete All Tasks is rated Medium on LeetCode.
What is the time complexity of LeetCode 2244. Minimum Rounds to Complete All Tasks?
The Python solution on this page runs in O(n).
What is the space complexity of LeetCode 2244. Minimum Rounds to Complete All Tasks?
The Python solution on this page uses O(n), where n is the length of the `tasks` array auxiliary space.
What topics does LeetCode 2244. Minimum Rounds to Complete All Tasks cover?
LeetCode 2244. Minimum Rounds to Complete All Tasks is tagged Greedy, Array, Hash Table and Counting on LeetCode.

Stuck on problems like this in a live interview?

Stealth Interview is a desktop app for macOS and Windows. It reads the problem off your screen and returns a working solution with a step-by-step explanation and its time and space complexity — invisible to screen sharing.

Get Stealth Interview