Schedule Maximum Jobs Within Deadlines
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests the classic greedy exchange-argument insight: sort jobs by deadline to maximise the number of unit-length jobs completed before their deadlines.
Schedule Maximum Jobs Within Deadlines
You are given a list of jobs. Each job takes exactly 1 unit of time to complete and has a deadline — the latest time slot by which it must finish.
Time slots are 1-indexed: slot t spans the interval (t-1, t]. A job with deadline d may be scheduled in any slot t where 1 ≤ t ≤ d, provided that slot is not already occupied by another job.
Your goal is to find the maximum number of jobs that can be completed on time if you schedule optimally.
Formally, given a list of deadlines (one per job), return the maximum number of jobs that can be scheduled such that every scheduled job finishes by its deadline.
Constraints
1 ≤ len(deadlines) ≤ 20001 ≤ deadlines[i] ≤ 2000- All deadlines are positive integers.
- Multiple jobs may share the same deadline.
- The order of jobs in the input does not matter.
Examples
Example 1
Input: max_jobs_scheduled([4, 2, 4, 3, 1])
Output: 4
Example 2
Input: max_jobs_scheduled([1, 1, 1])
Output: 1
Example 3
Input: max_jobs_scheduled([3, 3, 3, 3])
Output: 3
Hints
Hint 1
Think about what happens when you try to schedule the job with the earliest deadline first — can an exchange argument show this is never worse than any other order?
Hint 2
After sorting by deadline, greedily assign each job to the latest available slot that is still ≤ its deadline. This avoids unnecessarily blocking earlier slots for later jobs.
Hint 3
A simple boolean array of size max_deadline+1 tracks which slots are occupied; scanning from the job's deadline down to 1 finds the latest free slot in O(d) time.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight (Exchange Argument)
Sort jobs by deadline in ascending order. Suppose in an optimal schedule a job with a later deadline d2 is placed before a job with an earlier deadline d1 ≤ d2. Since both jobs take 1 unit of time and occupy one slot each, swapping them keeps the job with deadline d2 feasible (it moves to a slot ≤ d2), and keeps the job with deadline d1 feasible only if it now fits in the slot previously held by the other job — which it does since d1 ≤ d2 and the swapped slot ≤ d2 ≤ ... wait, more precisely: processing in deadline order and using the latest available slot ≤ deadline for each job ensures we never block an earlier deadline unnecessarily.
Algorithm
- Sort deadlines ascending.
- Maintain a boolean array
slot_free[1..max_deadline]. - For each job (in sorted order), find the latest free slot ≤ its deadline and assign it there, marking the slot occupied.
- Count scheduled jobs.
Assigning the latest possible slot preserves earlier slots for jobs with smaller deadlines that arrive later in sorted order (though sorting puts them first anyway). This greedy is correct by the exchange argument: any schedule that doesn't follow this order can be rearranged into one that does without losing jobs.
Why It Is Correct
- Feasibility: Every assigned job occupies a slot ≤ its deadline.
- Optimality: If a job cannot be scheduled (no free slot ≤ deadline), no rearrangement can fit it, because all slots up to its deadline are already occupied by jobs that each provably need one of those slots.
Complexity
- Time: O(n log n) for sorting + O(n · D) for slot search where D = max deadline (≤ 2000). Overall O(n · D).
- Space: O(D) for the slot array.
Reference Solution
from typing import List
def max_jobs_scheduled(deadlines: List[int]) -> int:
if not deadlines:
return 0
# Sort jobs by deadline ascending (exchange-argument greedy)
sorted_deadlines = sorted(deadlines)
max_deadline = max(sorted_deadlines)
# slot_free[t] == True means time slot t is available (1-indexed)
slot_free = [True] * (max_deadline + 1) # index 0 unused
count = 0
for d in sorted_deadlines:
# Find the latest free slot <= d
for t in range(d, 0, -1):
if slot_free[t]:
slot_free[t] = False
count += 1
break
return count