Job Sequencing with Deadlines
Schedule jobs with deadlines and profits to maximize total profit. Each job takes 1 unit time and has a deadline and profit. The greedy strategy is to sort jobs by profit (descending) and greedily schedule each job as late as possible before its deadline. This maximizes profit while respecting constraints. Used in task scheduling, CPU job management, and project planning.
Visualization
Interactive visualization for Job Sequencing with Deadlines
Job Sequencing with Deadlines
Jobs (sorted by profit):
• Time: O(n²) or O(n log n) with efficient data structure
• Greedy: schedule highest profit jobs first
• Assign to latest available slot before deadline
Interactive visualization with step-by-step execution
Implementation
1interface Job {
2 id: string;
3 deadline: number;
4 profit: number;
5}
6
7function jobSequencing(jobs: Job[], maxDeadline: number): [number, number] {
8 jobs.sort((a, b) => b.profit - a.profit);
9
10 const slots = new Array(maxDeadline).fill(null);
11 let count = 0;
12 let totalProfit = 0;
13
14 for (const job of jobs) {
15 for (let j = Math.min(maxDeadline, job.deadline) - 1; j >= 0; j--) {
16 if (slots[j] === null) {
17 slots[j] = job.id;
18 count++;
19 totalProfit += job.profit;
20 break;
21 }
22 }
23 }
24
25 return [count, totalProfit];
26}Deep Dive
Theoretical Foundation
Job Sequencing greedy approach: sort jobs by profit (highest first), then for each job, find the latest available slot before its deadline. Use a slot array to track which time slots are filled. For each job with deadline d and profit p, scan slots [min(maxDeadline, d)-1 down to 0] for first empty slot. If found, schedule job and mark slot occupied. This greedy choice (highest profit first) is locally optimal and leads to globally optimal solution. Time: O(n log n) sorting + O(n²) slot search = O(n²). Can be optimized to O(n log n) using Disjoint Set Union.
Complexity
Time
O(n²)
O(n²)
O(n²)
Space
O(n)
Applications
Industry Use
CPU task scheduling with deadlines
Project management (task prioritization)
Manufacturing job scheduling
Real-time systems scheduling
Resource allocation with time constraints
Earning maximization in time-bound tasks
Use Cases
Related Algorithms
Huffman Coding
Huffman Coding is a lossless data compression algorithm that creates optimal prefix-free variable-length codes based on character frequencies. Developed by David A. Huffman in 1952 as a student at MIT, it uses a greedy approach to build a binary tree where frequent characters get shorter codes. This algorithm is fundamental in ZIP, JPEG, MP3, and many compression formats.
Activity Selection Problem
Select the maximum number of non-overlapping activities from a set, where each activity has a start and end time. This classic greedy algorithm demonstrates the greedy choice property: always selecting the activity that finishes earliest leaves the most room for remaining activities. Used in scheduling problems, resource allocation, and interval management. Achieves optimal solution with O(n log n) time complexity.
Fractional Knapsack Problem
Given items with values and weights, and a knapsack with capacity, select items (or fractions thereof) to maximize total value. Unlike the 0/1 knapsack where items must be taken whole, the fractional knapsack allows taking fractions of items. The greedy approach of taking items in order of value-to-weight ratio yields the optimal solution in O(n log n) time. This demonstrates when greedy algorithms work vs. when dynamic programming is needed.
Minimum Platforms Problem
Find the minimum number of platforms required for a railway station given arrival and departure times of trains. No train should wait. The elegant solution uses the sorted merge technique: sort arrivals and departures separately, then use two pointers to track platforms needed at each moment. This greedy approach simulates the timeline efficiently in O(n log n) time, commonly asked in interviews.