Minimum Time Slots for Cooldown-Constrained Jobs
Greedy & IntervalsAuto-graded · 21 test casesLast updated
Compute the shortest timeline needed to run labeled jobs when equal labels must be separated by a cooldown, using frequency counting and a greedy frame argument.
Minimum Time Slots for Cooldown-Constrained Jobs
A single worker processes jobs one at a time. Time is divided into equal slots, and in each slot the worker either runs exactly one job or stays idle.
Each job is labeled with a single character. Jobs with the same label are interchangeable, and jobs may be run in any order. However, there is a cooldown rule: between any two runs of jobs with the same label, there must be at least cooldown slots in between (these slots may hold other jobs or be idle).
Given the list of job labels and the cooldown value, return the minimum total number of slots needed to finish every job.
Input
jobs: a list of single-character strings (labels). Labels may repeat.cooldown: a non-negative integer.
Output
Return an integer: the smallest number of slots in which all jobs can be completed while respecting the cooldown rule. If jobs is empty, return 0.
Notes
- Only the count matters; you do not need to return the actual schedule.
- With
cooldown = 0, no spacing is required, so every job simply takes one slot.
Constraints
0 <= len(jobs) <= 10^5- Each label is a single character (for example an uppercase letter).
0 <= cooldown <= 100
Examples
Example 1
Input: min_time_slots(['A', 'A', 'A', 'B', 'B', 'B'], 2)
Output: 8
Example 2
Input: min_time_slots(['A', 'A', 'A', 'B', 'B', 'B'], 0)
Output: 6
Example 3
Input: min_time_slots(['A', 'A', 'A', 'A', 'B', 'B', 'C', 'C'], 2)
Output: 10
Hints
Hint 1
Which label appears most often? Its occurrences force a minimum length on the timeline, regardless of what else you schedule.
Hint 2
Picture the most frequent label placed `max_count` times, with `cooldown` slots after each occurrence except the last. Think of each block of `cooldown + 1` slots as a frame.
Hint 3
Other labels fill the empty positions in the frames. Labels tied for the highest frequency each need a slot in the final partial frame. Also, the answer can never be smaller than the total number of jobs.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Count how many times each label appears. Let max_count be the largest frequency and max_types be the number of labels that reach that frequency.
Place the most frequent label first. Its max_count occurrences create max_count - 1 gaps, and each occurrence plus the cooldown slots after it forms a frame of cooldown + 1 slots. The final row contains only the labels tied for the maximum frequency, one slot each. This gives a timeline length of:
(max_count - 1) * (cooldown + 1) + max_types
The answer is the larger of this value and the total number of jobs, len(jobs).
Why it is correct
- Lower bound from the total: every job needs its own slot, so the answer is at least
len(jobs). - Lower bound from the frames: a label that appears
max_counttimes must span at least(max_count - 1) * (cooldown + 1) + 1slots. Each other label tied for the maximum also needs its last occurrence in the final row, so we needmax_typesslots there. - Achievability: if the frames have spare room, the remaining jobs fill frame positions without repeating a label inside a frame, so the cooldown holds, and leftover positions are idle. If there are more jobs than the frame layout can hold, the frames can be extended with the extra jobs and no idle slots are required, so the length equals
len(jobs).
Because both bounds can be reached, their maximum is optimal.
Complexity
- Time: O(T) to count labels, where T is the number of jobs.
- Space: O(K) for K distinct labels (at most the alphabet size).
Edge cases
- Empty list: return 0.
cooldown = 0: the formula yields at mostlen(jobs), so the answer islen(jobs).- A single label repeated
ctimes:(c - 1) * (cooldown + 1) + 1. - Many distinct labels: no idle slots are needed, so the answer is
len(jobs).
Reference solution
from collections import Counter
from typing import List
def min_time_slots(jobs: List[str], cooldown: int) -> int:
if not jobs:
return 0
counts = Counter(jobs)
max_count = max(counts.values())
max_types = sum(1 for c in counts.values() if c == max_count)
return max(len(jobs), (max_count - 1) * (cooldown + 1) + max_types)