Task Scheduler with Cooldown Periods
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Tests greedy scheduling with frequency counting to compute minimum execution time under per-task-type cooldown constraints.
You have a single-processor system that must execute a list of tasks. Each task is identified by an uppercase letter from 'A' to 'Z' in the array tasks. Tasks with the same letter represent the same task type, and each task takes exactly one time unit to complete.
There is a cooldown period represented by a non-negative integer n. After executing a task of a particular type, the processor must wait at least n time units before executing another task of that same type. During the cooldown, the processor can either execute a task of a different type or remain idle.
The processor executes exactly one task or remains idle for exactly one time unit in each cycle. You may schedule the tasks in any order you choose.
Return the minimum total time units required to complete all tasks.
Constraints:
1 <= tasks.length <= 10^4tasks[i]is an uppercase English letter from'A'to'Z'0 <= n <= 100
Examples
Example 1
Input: min_schedule_time(['A', 'A', 'A', 'B', 'B', 'B'], 2)
Output: 8
Example 2
Input: min_schedule_time(['A', 'A', 'A', 'B', 'B', 'B'], 0)
Output: 6
Example 3
Input: min_schedule_time(['A', 'A', 'A', 'A', 'A', 'A', 'B', 'C', 'D', 'E', 'F', 'G'], 2)
Output: 16
Hints
Hint 1
The most frequent task type determines the minimum schedule length. Think about how many 'slots' you need between its occurrences.
Hint 2
Build a conceptual frame: place the most frequent task with exactly n slots between each occurrence. How many frames do you create?
Hint 3
If multiple task types share the maximum frequency, they all need a position in the final segment. Count how many tie for first place.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The key insight is that we don't need to simulate the actual schedule. Instead, we can compute the minimum time directly from the task frequency distribution.
The most frequent task type dictates the structure of an optimal schedule. If a task type appears f times, we must space these occurrences with at least n slots between them. This creates f - 1 "frames" of width n + 1, followed by a final task:
X _ _ _ X _ _ _ X _ _ _ X
For f = 4 and n = 3, we have 3 frames of width 4, plus one final position.
All other task types can be placed in the gaps within these frames. If multiple task types share the maximum frequency f, each needs a slot in that final position, extending it from 1 to k positions (where k is the count of tasks with maximum frequency).
The formula becomes: (f - 1) × (n + 1) + k
However, if we have many different task types, the gaps may overflow and we can pack tasks more tightly without any idle time. In that case, the answer is simply the total number of tasks.
Therefore: answer = max(total_tasks, (f - 1) × (n + 1) + k)
Why It Works
Lower bound: Any schedule must be at least as long as the total number of tasks. Additionally, the most frequent task type forces a minimum spacing structure that requires (f - 1) × (n + 1) + k time units.
Achievability: We can construct a valid schedule by filling a grid row-by-row, placing the highest-frequency tasks first in separate columns, then filling remaining slots with other tasks. This construction achieves the formula.
Complexity
- Time complexity: O(m) where m is the number of tasks. We make one pass to count frequencies and one pass over at most 26 counters to find the maximum.
- Space complexity: O(1) since we use a fixed-size array of 26 counters for the English alphabet.
from typing import List
def min_schedule_time(tasks: List[str], n: int) -> int:
"""
Return the minimum number of time units required to execute all tasks
with a cooldown period of n between tasks of the same type.
"""
# Count frequency of each task type
freq = [0] * 26
for task in tasks:
freq[ord(task) - ord('A')] += 1
# Find the maximum frequency and count how many tasks have it
max_frequency = max(freq)
tasks_with_max_freq = sum(1 for count in freq if count == max_frequency)
# Calculate minimum time based on the frame structure
# (max_frequency - 1) frames of width (n + 1), plus the final segment
frame_time = (max_frequency - 1) * (n + 1) + tasks_with_max_freq
# The answer is either the frame time or total tasks, whichever is larger
return max(len(tasks), frame_time)