Minimum workers with first-free job assignment
Jobs arrive as (job_id, start, end) with half-open times [start, end). Workers are numbered 0, 1, 2, ...
Part 1. Process jobs by start time (ties by job id). Each job goes to the lowest-numbered worker that is free at its start; hire a new worker only when every existing one is busy. Return the number of workers used and the worker of each job.
Part 2. Now there are exactly k workers. Jobs are served first in, first out: a job starts at the later of its release time, the previous job's start, and the moment a worker is free (again the lowest-numbered one among those free then). Each job keeps its duration. Return each job's worker, start and finish.
Example: jobs a [0, 10), b [2, 5), c [5, 12), d [10, 11) need 2 workers: a → 0, b → 1, c → 1, d → 0. With k = 1, b runs [10, 13), c [13, 20) and d [20, 21).
def assign_workers(jobs):
"""-> (workers needed, {job_id: worker})"""
def schedule_with_pool(jobs, k):
"""-> {job_id: (worker, start, finish)}"""
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. Free with an account: one full answer per company.
