Minimum Rounds to Complete All Tasks — LeetCode 2244 Python Solution
MediumGreedyArrayHash TableCounting
- Problem
- #2244
- Pattern
- Greedy
- Reading time
- 2 min
- Source
- leetcode.com
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 ansComplexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(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
LeetCode 621Task SchedulerMediumLeetCode 1054Distant BarcodesMediumLeetCode 1090Largest Values From LabelsMediumLeetCode 1481Least Number of Unique Integers after K RemovalsMediumLeetCode 1775Equal Sum Arrays With Minimum Number of OperationsMediumLeetCode 2131Longest Palindrome by Concatenating Two Letter WordsMedium
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.