Maximize Completed Jobs with Deadlines
Greedy & IntervalsAuto-graded · 17 test casesLast updated
Tests greedy scheduling with deadlines: select and order unit-length jobs to maximize total profit, applying an exchange argument to prove optimality.
Maximize Completed Jobs with Deadlines
You are managing a single-machine workshop. A list of jobs arrives, each requiring exactly one time slot to complete. Time slots are numbered 1, 2, 3, ….
Each job has:
- A deadline
d(positive integer): the job must finish on or before slotd. - A profit
p(non-negative integer): earned only if the job is completed by its deadline.
You may schedule at most one job per time slot, and you are free to leave slots empty. A job completed in slot t satisfies its deadline if and only if t <= d.
Your goal is to choose a subset of jobs and assign each to a distinct time slot (respecting deadlines) so that the total profit is maximised. Return that maximum total profit.
If multiple jobs share the same deadline and profit, they are interchangeable — the answer is the same regardless of which one you pick.
Constraints
1 <= len(jobs) <= 2000- Each element of
jobsis a two-element list[d, p]. 1 <= d <= 20000 <= p <= 10_000- There is always at least one job (the list is non-empty).
Examples
Example 1
Input: max_profit_schedule([[4, 20], [1, 10], [1, 40], [1, 30]])
Output: 60
Example 2
Input: max_profit_schedule([[2, 100], [2, 19], [1, 27], [3, 25], [3, 15]])
Output: 152
Example 3
Input: max_profit_schedule([[1, 5], [1, 5], [1, 5]])
Output: 5
Hints
Hint 1
Sort jobs by profit in descending order so you always try to include the most valuable job first.
Hint 2
For each job (in profit order), greedily assign it to the latest available slot that is still <= its deadline. This keeps earlier slots free for jobs with tighter deadlines.
Hint 3
Use a simple boolean array of size max_deadline+1 to track which slots are occupied, and scan backwards from the job's deadline to find a free slot in O(d) time per job.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Key Insight: Greedy by Profit with Latest-Slot Assignment
This is the classic job sequencing with deadlines problem. The greedy strategy is:
- Sort jobs by profit in descending order. This ensures we always consider the most valuable jobs first.
- For each job, assign it to the latest available time slot that does not exceed its deadline. Using the latest valid slot is critical — it preserves earlier slots for jobs with tighter deadlines that we might encounter later (even though those jobs have lower profit, greedily we still want to include as many high-value jobs as possible).
- Skip a job if no valid slot exists (all slots up to its deadline are occupied).
Why This Is Correct (Exchange Argument)
Suppose an optimal solution does not include the highest-profit unchosen job J but does include some lower-profit job J' in a slot s <= deadline(J). We can swap J' for J in slot s (since s <= deadline(J') implies s <= deadline(J) only if deadlines align — more precisely, the greedy never displaces a job into an invalid slot because we always place J in the latest free slot ≤ its deadline). A formal exchange argument shows that any schedule that omits a job the greedy includes can be transformed, without decreasing profit, into one that agrees with the greedy choice.
Algorithm Details
- Find
max_deadlineto size the slot-tracking array. - For each job in profit-descending order, scan backwards from
min(deadline, max_deadline)to find the latest free slot. - Mark that slot used and accumulate the profit.
Complexity
- Time: O(n log n) for sorting + O(n · D) for slot assignment, where D = max deadline ≤ 2000. With n ≤ 2000 and D ≤ 2000, the worst case is O(4 × 10⁶), well within limits.
- Space: O(D) for the slot-used array.
Reference Solution
def max_profit_schedule(jobs: list) -> int:
if not jobs:
return 0
# Sort jobs by profit descending (greedy: always try to include highest-value job first)
sorted_jobs = sorted(jobs, key=lambda x: x[1], reverse=True)
max_deadline = max(d for d, p in jobs)
# slot_used[t] = True means slot t is already taken (1-indexed)
slot_used = [False] * (max_deadline + 1)
total_profit = 0
for deadline, profit in sorted_jobs:
# Try to assign to the latest free slot <= deadline
slot = min(deadline, max_deadline)
while slot >= 1 and slot_used[slot]:
slot -= 1
if slot >= 1:
slot_used[slot] = True
total_profit += profit
return total_profit