Largest Set of Compatible Time Slots on One Worker
Greedy & IntervalsAuto-graded · 19 test casesLast updated
Find the largest subset of half-open time intervals that can run on one worker without overlap, testing greedy interval selection by earliest finish.
Largest Set of Compatible Time Slots on One Worker
A single worker can handle only one job at a time. You are given a list of jobs, where each job is a pair [start, end] describing the half-open time range from start up to, but not including, end.
Two jobs conflict if their time ranges share any moment. A job that ends at time t does not conflict with a job that begins at time t, so [1, 3] and [3, 5] can both be chosen.
Choose a subset of the jobs so that no two chosen jobs conflict, and make that subset as large as possible. Return the number of jobs in it.
Input
jobs: a list of[start, end]pairs of integers, withstart < endfor each pair.
Output
- An integer: the maximum number of pairwise non-conflicting jobs. Return
0ifjobsis empty.
Rules and edge cases
- Jobs may be given in any order, and identical jobs may appear more than once. Identical jobs conflict with each other, so at most one copy of them can be chosen.
- Times may be negative or large.
- Do not rely on the input being sorted. Avoid assuming the caller's list may be modified.
Constraints
0 <= len(jobs) <= 10^5- Each job is
[start, end]withstart < end. startandendare integers that fit in a standard 64-bit signed integer.
Examples
Example 1
Input: max_compatible_jobs([[1, 3], [1, 5], [4, 6]])
Output: 2
Example 2
Input: max_compatible_jobs([[0, 2], [2, 4], [1, 3], [4, 5]])
Output: 3
Example 3
Input: max_compatible_jobs([[1, 10], [2, 3], [3, 4], [4, 5]])
Output: 3
Hints
Hint 1
Think about which job to commit to first so that the most time remains for the others. Sorting by one of start, end, or length gives different strategies; only one is always right.
Hint 2
Sort the jobs by end time. Keep track of the moment the worker becomes free, and accept a job whenever its start is at or after that moment.
Hint 3
Because intervals are half-open, a job starting exactly when the last chosen one ends is allowed, so compare with >= rather than >.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Use a greedy strategy: always pick the compatible job that finishes earliest.
- Sort the jobs by end time (without modifying the input).
- Keep a variable
free_at, the time the worker becomes available, initially negative infinity. - Go through the sorted jobs. If a job's
start >= free_at, take it: increase the count and setfree_atto itsend. Otherwise skip it. - Return the count.
The >= comparison reflects the half-open ranges: a job beginning exactly when the previous one ends does not overlap it.
Why it is correct
Let the greedy picks be g1, g2, ..., gk, and let any valid optimal selection be o1, o2, ..., om, both ordered by end time. We show by induction that end(gi) <= end(oi) for every i <= k.
- For
i = 1, g1 has the smallest end time of all jobs. - If
end(gi) <= end(oi), theno(i+1)starts at or afterend(oi), hence at or afterend(gi), so it was available to the greedy when it choseg(i+1). The greedy takes the available job with the smallest end, soend(g(i+1)) <= end(o(i+1)).
If m > k, job o(k+1) would start at or after end(gk) and the greedy would have taken some further job, contradicting that it stopped at k. So k = m, and the greedy is optimal.
Other rules fail: earliest start first fails on [[1,10],[2,3],[4,5]], and shortest job first fails on [[1,5],[4,7],[6,10]].
Complexity
- Time: O(n log n) for the sort, plus O(n) for the scan.
- Space: O(n) for the sorted copy.
Reference solution
from typing import List
def max_compatible_jobs(jobs: List[List[int]]) -> int:
count = 0
free_at = float('-inf')
for start, end in sorted(jobs, key=lambda job: job[1]):
if start >= free_at:
count += 1
free_at = end
return count