KatInterview

Minimum Time Slots for Cooldown-Constrained Jobs

Greedy & IntervalsAuto-graded · 21 test cases
MediumAuto-gradedGreedy & Intervalsgreedycountingscheduling

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