KatInterview

Minimum Shift Count for Concurrent Task Scheduling

Greedy & IntervalsModel answer
MediumModel answerGreedy & Intervalsgreedyintervalsheap

Last updated

Given tasks with time windows and resource demands, find the fewest parallel workers needed so no worker exceeds capacity K at any moment.

Background

You run an operations platform that assigns jobs to workers. Each job runs during a specific time window and consumes some amount of a shared resource while active. A worker can hold multiple jobs simultaneously, but the combined resource usage across all active jobs on that worker must never exceed a fixed limit at any point in time. Jobs that do not overlap can be assigned to the same worker back-to-back.

Your goal is to figure out how few workers you need to cover every job.

Task

Write a function that, given a list of jobs and a per-worker capacity limit, returns the minimum number of workers required.

Each job is described by three integers: [start, end, demand], where the job is active during the half-open interval [start, end) — a job ending at time t frees its demand before a new job starting at t begins. The demand represents how much of the worker's capacity that job consumes while active.

def min_workers(jobs: List[List[int]], K: int) -> int

Constraints

  • 1 <= len(jobs) <= 10^4
  • 0 <= start < end <= 10^6
  • 1 <= demand <= K
  • 1 <= K <= 100

Examples

Example 1

jobs = [[1, 5, 2], [2, 6, 2], [7, 8, 1]], K = 3
Output: 2

Jobs [1,5,2] and [2,6,2] overlap during [2,5) and have a combined demand of 4, which exceeds K=3. They must go to separate workers. Job [7,8,1] does not overlap either of the first two, so it can share a worker with one of them — no third worker is needed.

Example 2

jobs = [[0, 10, 3], [0, 10, 3], [0, 10, 3]], K = 3
Output: 3

All three jobs span the exact same window. Each fills a worker entirely. Three workers are required.

Example 3

jobs = [[1, 3, 1], [3, 5, 1], [1, 5, 1]], K = 2
Output: 2

Jobs [1,3,1] and [3,5,1] do not overlap (half-open intervals) so they fit the same worker back-to-back. Job [1,5,1] overlaps both, and combined demand with either active segment is 2 which equals K, so it can share with the first worker. Output is 1... wait, check carefully: at t=1, worker A carries [1,3,1] (demand 1) and [1,5,1] (demand 1) — total 2 = K. At t=3, [1,3,1] ends, then [3,5,1] starts on worker A — total still 2 = K. All three jobs fit on one worker. Output: 1.

What to return

Return a single integer: the minimum number of workers.

Edge Cases to Consider

  • A single job always needs exactly one worker.
  • Jobs whose windows share only an endpoint (e.g., [1,5) and [5,9)) do not overlap, so they can share a worker.
  • A job with demand == K fills a worker entirely while active but that worker is reusable afterward.
  • All jobs sharing the same window turns this into pure bin packing.

Hints

Hint 1

Sort jobs by start time. Among jobs starting at the same time, process higher-demand jobs first — this mirrors the 'first-fit decreasing' strategy from bin packing and tends to leave smaller gaps for later jobs.

Hint 2

Use a min-heap keyed on end time to track active jobs per worker. Before assigning a new job starting at time s, pop all heap entries with end <= s and subtract their demand from the corresponding worker's current load. A job ending at exactly s does not conflict with one starting at s.

Hint 3

With K <= 100, you can keep an array of sets indexed by current load level (0 through K). To find the best-fit worker for a job with demand c, scan from load level K-c down to 0 and pick any worker in the first non-empty bucket. This gives O(K) lookup per job without scanning all workers.