Job Scheduling by Deadline
Sort jobs by profit (highest first). For each job, assign it to the latest free slot before its deadline. This maximizes total profit because you handle the most valuable jobs first and give them the best possible slots.
How It Works
Job sequencing with deadlines schedules unit-length jobs, each with a deadline and a profit, to maximize total profit. Sort jobs by profit descending, then place each job into the latest still-free time slot at or before its deadline; if no slot is free, drop the job. Handling the most valuable jobs first and pushing them as late as possible keeps earlier slots open for tighter future deadlines, and an exchange argument shows no schedule can beat this.
With an array of slots the placement scan gives O(n^2) worst case; a disjoint-set union that jumps straight to the nearest free earlier slot brings it to near O(n log n) including the sort. This deadline-plus-value structure — sort by one key, greedily assign against the other — recurs throughout scheduling problems.
Step-by-Step Visualization
Code
static int jobScheduling(int[][] jobs) {
Arrays.sort(jobs, (a, b) -> b[1] - a[1]); // Sort by profit desc
int maxDeadline = 0;
for (int[] j : jobs) maxDeadline = Math.max(maxDeadline, j[0]);
boolean[] slots = new boolean[maxDeadline + 1];
int profit = 0;
for (int[] job : jobs) {
for (int d = job[0]; d >= 1; d--) {
if (!slots[d]) {
slots[d] = true;
profit += job[1];
break;
}
}
}
return profit;
}Tips & Gotchas
Practice Problems
- 1Maximum Profit in Job Scheduling
- 2Course Schedule III
- 3Maximum Earnings From Taxi
- 4Single-Threaded CPU
About the Task Scheduling Pattern
Assign tasks to time slots or workers to maximize value or meet deadlines. Sort by a key metric (deadline, value, ratio), then greedily assign.
Greedy is NOT 'try the obvious thing'. It works only when local optimality guarantees global optimality. Sort first (by end time, deadline, ratio), then pick greedily. If greedy fails, try DP.
Common Greedy Interview Problems
- Jump Game
- Activity Selection
- Meeting Rooms II
- Gas Station
- Candy
- Task Scheduler
- Partition Labels
Frequently Asked Questions
Why place each job in the latest free slot rather than the earliest?
The job only requires completion by its deadline, so occupying the latest feasible slot preserves earlier slots for jobs with smaller deadlines that have no other options. Filling early slots first can lock out a tight-deadline job that appears later in profit order.
When do jobs with different durations break this greedy?
Unit length is essential: with variable durations and profits, the interaction between time consumed and value gained defeats simple profit ordering. Weighted job scheduling with durations is solved by DP over end-time-sorted jobs with binary search, in O(n log n).
How does Course Schedule III adapt the deadline idea?
It processes courses sorted by deadline and greedily takes each one, but when the running time overflows a deadline it evicts the longest course taken so far using a max-heap. Swapping a long course for a shorter one never reduces the count, which is the exchange argument in heap form.