Schedule Maximum Jobs Before Their Deadlines
Greedy & IntervalsAuto-graded · 16 test casesLast updated
Tests greedy scheduling with deadlines: select the maximum number of unit-length jobs, each with a profit and deadline, maximizing total profit within deadline slots.
Schedule Maximum Profit Jobs
You are managing a single-machine job queue. There are n jobs, each taking exactly one unit of time to complete. Time slots are numbered 1, 2, 3, …. Each job has:
- A deadline
d(the job must finish at or before time slotd) - A profit
p(earned only if the job is scheduled before or on its deadline)
The machine can process at most one job per time slot.
Your task is to select a subset of jobs and assign each to a distinct time slot ≤ its deadline, maximizing total profit. Return the maximum total profit achievable and the number of jobs scheduled.
Function Signature
def schedule_jobs(jobs: list[list[int]]) -> list[int]:
jobs: a list of[deadline, profit]pairs.- Returns
[max_profit, num_jobs_scheduled].
How the greedy works
Sort jobs by profit descending. For each job (greediest first), try to assign it to the latest available slot that is ≤ its deadline. If such a slot exists, schedule it; otherwise skip.
Constraints
0 ≤ len(jobs) ≤ 20001 ≤ deadline ≤ 10000 ≤ profit ≤ 10^6- If
jobsis empty, return[0, 0]. - Profits are not necessarily distinct.
- Multiple jobs may share the same deadline.
Examples
Example 1
Input: schedule_jobs([[4, 20], [1, 10], [1, 40], [1, 30]])
Output: [60, 2]
Example 2
Input: schedule_jobs([[2, 100], [1, 19], [2, 27], [1, 25], [3, 15]])
Output: [142, 3]
Example 3
Input: schedule_jobs([])
Output: [0, 0]
Hints
Hint 1
Sort jobs by profit in descending order so you always try to place the most valuable job first.
Hint 2
Use a boolean array of size (max_deadline + 1) to track which time slots are still free; for each job scan from its deadline down to slot 1 for the latest free slot.
Hint 3
If no free slot ≤ the job's deadline exists, skip that job entirely and move on to the next.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach: Greedy by Profit with Latest-Slot Assignment
Key Insight
This is the classic "job sequencing with deadlines" problem. The greedy argument is:
- Sort jobs by profit descending. We should always try to schedule the most valuable job first, since it contributes the most if it fits.
- Assign each job to the latest possible free slot ≤ its deadline. Using the latest available slot preserves earlier slots for jobs with tighter deadlines that come later in our sorted order.
This greedy is provably optimal by an exchange argument: if an optimal solution skips a high-profit job in favor of a lower-profit job, we can swap them without violating any deadline, yielding a solution at least as good. Repeating the argument gives us the greedy solution.
Algorithm
- If
jobsis empty return[0, 0]. - Sort
jobsbyprofitdescending. - Find
max_deadlineand create a booleanslotsarray of that size (1-indexed). - For each
(deadline, profit)in sorted order:- Scan from
min(deadline, max_deadline)down to1for the first free slot. - If found, mark it occupied, add profit, increment count.
- Scan from
- Return
[total_profit, count].
Correctness
The latest-slot heuristic is critical: placing a job as late as possible within its window keeps earlier slots free for more-constrained jobs that appear later in our traversal (even though they have lower profit, they may have no other option). The profit-descending sort ensures we never sacrifice a high-value job for a low-value one.
Complexity
- Time: O(n log n) for sorting + O(n · D) for slot search, where D = max deadline ≤ 1000. With n ≤ 2000 and D ≤ 1000 this is at most ~2 × 10^6 operations — well within limits.
(A Union-Find approach can reduce slot search to near O(1) amortized, but is unnecessary here.) - Space: O(D) for the slots array.
Reference Solution
from typing import List
def schedule_jobs(jobs: List[List[int]]) -> List[int]:
if not jobs:
return [0, 0]
# Sort by profit descending (greedy: pick highest profit first)
sorted_jobs = sorted(jobs, key=lambda x: -x[1])
max_deadline = max(d for d, p in jobs)
# slots[i] = True means slot i is occupied (1-indexed)
slots = [False] * (max_deadline + 1)
total_profit = 0
count = 0
for deadline, profit in sorted_jobs:
# Find the latest free slot <= deadline
slot = min(deadline, max_deadline)
while slot >= 1 and slots[slot]:
slot -= 1
if slot >= 1:
slots[slot] = True
total_profit += profit
count += 1
return [total_profit, count]