DSA Notes
Solve the Job Sequencing problem using a greedy approach — maximize profit by sorting by profit and assigning each job to the latest available time slot before its deadline.
Problem Statement
Given N jobs, each with a deadline and a profit (earned only if the job is completed before or on its deadline), and the constraint that each job takes exactly 1 unit of time, find a sequence that maximizes total profit. Each time slot can hold at most one job.
Greedy Intuition
Since we want to maximize profit, we should prioritize high-profit jobs. But a high-profit job is useless if we cannot schedule it before its deadline. The greedy strategy:
- Sort jobs by profit in descending order (most profitable first).
- For each job, assign it to the latest available slot before its deadline.
Why the latest slot? Because scheduling early wastes precious early slots that might be needed by other jobs with tighter deadlines. Scheduling as late as possible gives maximum flexibility for remaining jobs.
Dry Run Example
| Jobs | (ID, Deadline, Profit) |
| J1 | deadline=4, profit=20 |
| J2 | deadline=1, profit=10 |
| J3 | deadline=1, profit=40 |
| J4 | deadline=1, profit=30 |
| Sorted by profit (descending) | J3(40), J4(30), J1(20), J2(10) |
| Max deadline = 4, so time slots | [_, _, _, _] (slots 1,2,3,4) |
| Latest slot ≤ 1: slot 1 is free | assign J3 to slot 1. |
| Slots | [J3, _, _, _] |
| Latest slot ≤ 1: slot 1 is taken | NO slot available. SKIP. |
| Latest slot ≤ 4: slot 4 is free | assign J1 to slot 4. |
| Slots | [J3, _, _, J1] |
| Latest slot ≤ 1: slot 1 is taken | NO slot available. SKIP. |
| Result | Jobs {J3, J1}, Total profit = 40 + 20 = 60. |
Another Example (More Complex)
| Jobs | (ID, Deadline, Profit) |
| A | deadline=2, profit=100 |
| B | deadline=1, profit=19 |
| C | deadline=2, profit=27 |
| D | deadline=1, profit=25 |
| E | deadline=3, profit=15 |
| Sorted by profit | A(100), C(27), D(25), B(19), E(15) |
| Slots | [_, _, _] (max deadline = 3) |
| A (deadline=2): Latest free ≤ 2 | slot 2. Slots: [_, A, _] |
| C (deadline=2): Latest free ≤ 2 | slot 1. Slots: [C, A, _] |
| D (deadline=1): Latest free ≤ 1 | slot 1 taken. SKIP. |
| B (deadline=1): Latest free ≤ 1 | slot 1 taken. SKIP. |
| E (deadline=3): Latest free ≤ 3 | slot 3. Slots: [C, A, E] |
| Result | {C, A, E}, Profit = 27 + 100 + 15 = 142 |
| Sequence: C(slot 1) | A(slot 2) → E(slot 3) |
Python Implementation
C++ Implementation
Optimized Approach: Union-Find (Disjoint Set)
The naive approach searches for a free slot in O(D) per job, giving O(N×D) total. Using Union-Find, we can find the latest free slot in nearly O(1) amortized:
Complexity with DSU: O(N log N) for sorting + O(N × α(D)) for scheduling ≈ O(N log N) total.
Complexity Analysis
| Approach | Time | Space |
|---|---|---|
| Naive (search for slot) | O(N × D) | O(D) |
| With Union-Find | O(N log N + N × α(D)) ≈ O(N log N) | O(D) |
Where N = number of jobs, D = maximum deadline.
Correctness Argument
The greedy strategy (highest profit first, latest available slot) is optimal because:
- Higher-profit jobs should always be preferred over lower-profit ones.
- Assigning to the latest slot preserves earlier slots for jobs with tighter deadlines.
- An exchange argument shows: if an optimal solution differs from greedy, swapping any job for a higher-profit one in its slot cannot decrease total profit.
Variations
- Weighted Job Scheduling (variable duration): Jobs have different processing times. Requires DP, not greedy.
- Job Sequencing with Machine Assignment: Multiple machines available. Each machine has independent slot arrays.
- Minimizing Lateness: Instead of profit, minimize maximum lateness. Greedy criterion: earliest deadline first.
Real-World Applications
- Freelance project management: Accept highest-paying projects that meet deadlines
- CPU task scheduling: Real-time OS scheduling with hard deadlines
- Order fulfillment: E-commerce warehouse prioritizing high-value orders
- Publishing deadlines: Editors choosing which articles to include before print deadline
- Ad campaign scheduling: Running most profitable ads within campaign windows
Key Takeaways
Job Sequencing demonstrates greedy scheduling at its best: sort by profit, assign to the latest feasible slot. The Union-Find optimization is a classic data structure application that reduces slot-finding from O(D) to nearly O(1). This problem frequently appears in placement interviews — remember the "latest free slot" insight, it is the key that makes the greedy approach work.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Job Sequencing with Deadlines.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this Data Structures & Algorithms topic.
Search Terms
data-structures-algorithms, data structures & algorithms, data, structures, algorithms, greedy, job, sequencing
Related Data Structures & Algorithms Topics