Maximize Jobs Completed Before Their Deadlines
Greedy & IntervalsAuto-graded · 18 test casesLast updated
Tests greedy scheduling with deadlines: select and order unit-duration jobs to maximize the count of jobs finished on or before their individual deadlines.
Maximize Jobs Completed Before Their Deadlines
You are given a list of jobs. Each job takes exactly 1 unit of time to complete and has a deadline by which it must finish. Time slots are numbered starting at 1 (so a job with deadline d must be scheduled in a slot ≤ d).
You may schedule at most one job per time slot, and each job may be scheduled at most once. You want to choose a subset of jobs and assign each chosen job to a distinct time slot such that every chosen job finishes on or before its deadline.
Return the maximum number of jobs that can be completed on time.
Details
- Jobs are given as a list of integers
deadlines, wheredeadlines[i]is the deadline of the i-th job. - You do not need to return the schedule itself — only the count of jobs that can be completed on time.
- If two jobs could both be included but only one fits, prefer the one that leaves more scheduling room for others (this is handled automatically by the greedy strategy; you just return the count).
Constraints
0 ≤ len(deadlines) ≤ 20001 ≤ deadlines[i] ≤ 2000- All deadlines are positive integers.
Examples
Example 1
Input: max_jobs_on_time([3, 1, 2])
Output: 3
Example 2
Input: max_jobs_on_time([1, 1, 1])
Output: 1
Example 3
Input: max_jobs_on_time([])
Output: 0
Hints
Hint 1
Sort jobs by deadline in ascending order — greedily scheduling earlier-deadline jobs first avoids blocking later slots unnecessarily.
Hint 2
Keep a counter of how many slots have been used so far. For each job (in deadline order), ask: is the number of slots used so far strictly less than this job's deadline? If yes, schedule it.
Hint 3
This greedy works because every job takes exactly 1 unit of time, so the only question is whether a free slot exists at or before the deadline; sorting ensures we never waste an early slot on a late-deadline job when an early-deadline job still needs it.
Worked solution
Easy Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight
Because every job takes exactly 1 unit of time, scheduling is purely about counting available slots. A job with deadline d can be placed in any of the slots 1, 2, …, d. The classic greedy argument (an exchange argument) shows that:
Sort jobs by deadline ascending, then greedily include each job if a free slot still exists at or before its deadline.
Why it is correct (exchange argument):
Suppose an optimal solution skips some job j (deadline d_j) and instead includes a later-deadline job j' (deadline d_{j'} ≥ d_j) in a slot s ≤ d_j. We can swap j' for j in slot s — since s ≤ d_j ≤ d_{j'}, job j' can simply be moved to whatever slot j would have occupied, or dropped, without decreasing the count. Repeating this argument shows the greedy choice is always safe.
Implementation:
After sorting, maintain scheduled — the number of jobs selected so far. For each job with deadline d:
- If
scheduled < d, there aredpossible slots and fewer thandare taken, so at least one slot≤ dis free. Schedule the job:scheduled += 1. - Otherwise, skip it.
Return scheduled.
Complexity
| Time | O(n log n) — dominated by the sort |
| Space | O(n) — for the sorted copy (or O(1) extra if sorted in place) |
Reference Solution
def max_jobs_on_time(deadlines: list) -> int:
sorted_deadlines = sorted(deadlines)
scheduled = 0
for d in sorted_deadlines:
if scheduled < d:
scheduled += 1
return scheduled
Worked Examples
[3, 1, 2]→ sorted:[1, 2, 3]. Slot 1 for deadline-1, slot 2 for deadline-2, slot 3 for deadline-3. Count = 3.[1, 1, 1]→ sorted:[1, 1, 1]. First job uses slot 1. Second:1 < 1is false — skip. Third: skip. Count = 1.[]→ no jobs. Count = 0.