#2244
Minimum Rounds to Complete All Tasks
MediumArrayHash TableGreedyCountingHash MapArray
Approaches
Brute ForceOptimal
Complexity Comparison
| Brute Force | Optimal Solution★ | |
|---|---|---|
| Time | O(n²) | O(n) |
| Space | O(1) | O(n) |
💡
Intuition
Time O(n)Space O(n)
The optimal solution uses a frequency map to count tasks and then calculates the minimum rounds needed based on the counts. This approach is efficient and leverages mathematical properties to minimize rounds.
⚙️
Algorithm
4 steps- 1Step 1: Count the occurrences of each difficulty level using a frequency map.
- 2Step 2: For each frequency, check if it's possible to complete the tasks (i.e., frequency should not be 1).
- 3Step 3: Calculate the minimum rounds using the formula: rounds += (freq + 2) // 3.
- 4Step 4: Return the total rounds.
solution.py10 lines
1from collections import Counter
2
3def minRounds(tasks):
4 count = Counter(tasks)
5 rounds = 0
6 for freq in count.values():
7 if freq == 1:
8 return -1
9 rounds += (freq + 2) // 3
10 return roundsℹ
Complexity note: The time complexity is O(n) because we only traverse the tasks array once to count frequencies and then once more to calculate rounds, making it linear. The space complexity is O(n) due to the storage of frequencies.
- 1You cannot complete a task if it appears only once.
- 2Using combinations of 2s and 3s effectively minimizes the number of rounds.
Solutions and explanations are original Tejav content. Problem titles © LeetCode — use the LeetCode button above for the full problem statement.