Job sequencing
Intermediate · 10 min · code in PHP, JavaScript, Java and Go
Where you will meet this in real life
Job sequencing picks which jobs to do when each earns a profit but must be done by a deadline and each takes one time slot. The idea is to prefer the most profitable jobs and place each as late as possible before its deadline.
Practical use cases
- Web app: Choosing orders before the delivery cut-off. A kitchen app has orders with a profit and a "deliver by" slot, and the rider can take one per slot. Taking the most valuable orders first and putting each in the latest free slot before its cut-off earns the most for the evening.
- Web app: Print and render queue. A print shop site has jobs with fees and promised deadlines, and one printer. The system schedules the jobs so the highest-paying ones always meet their deadlines.
- Web app: Sprint planning tool. A team has stories with business value and release dates. The tool suggests which to do in each day so the most value ships before the dates pass.
- System: Ad bidding and delivery slots. Ad systems fill limited slots before a deadline with the highest bids, and courier systems do the same with delivery windows. Each best item goes as late as its cut-off allows, which keeps earlier slots free for tighter deadlines.
The story: Kabir's Pocket-Money Week
Kabir had a week of small jobs from the neighbours: washing a scooter, carrying groceries, watering plants, and more. Each job took exactly one day and paid some rupees, but each neighbour also said, "Do it by this day or forget it!"
On the first day he took the job that was easiest. By day three he saw the best-paying job had a deadline of day two. Gone. He had wasted a day on a ₹15 job.
Mira drew a calendar with three boxes and told him a rule: "Look at the jobs from the highest pay. Put each one on the latest day that still meets its deadline. That keeps the early days free for jobs with tight deadlines. If no box is free, skip the job."
Kabir tried it. Job a (₹100, deadline day 2) went into day 2. Job c (₹27, deadline 2) took day 1. Jobs d and b needed day 1, which was taken, so they were dropped. Job e (₹15, deadline 3) took day 3. Total ₹142, the best possible. This is job sequencing with deadlines.
How to spot this problem, and where you need it
You have more jobs than slots, each with a reward and a deadline, and you must choose and order them so the total reward is as high as possible.
Signs you need it
- The wording has "deadline", "profit" and "one job per time unit" together.
- Doing jobs in order of arrival or deadline alone wastes valuable jobs, leaving high-profit work undone.
- Some jobs will inevitably be dropped, and the question is which ones.
Where to use it in a project
- Order, print, render or delivery queues with cut-off times and different values.
- Planning tools that decide what fits before a release or a deadline.
The solution
Sort jobs by profit from highest to lowest. For each job in that order, look for a free slot at its deadline or any earlier; take the latest free one and book the job there. If no free slot exists at or before the deadline, drop the job. Placing each job late keeps earlier slots open for tighter deadlines, so more valuable jobs fit in total.
Payoff: Before: first-come order earning Rs 2,100 in a night. After: profit-first with latest slots earns Rs 3,400 with the same rider.
Explain it like I am 8
You have a few homework pages, each worth some stars and each due on a certain day. You can do one a day. Do the most valuable ones first, and put each on the last possible day so the early days are still free for the urgent ones.
How it works, step by step
- Sort jobs by profit, highest first.
- Make an empty calendar with one slot for each day up to the largest deadline.
- For each job, look at its deadline day and walk backwards to find a free slot.
- If you find one, put the job there and add its profit. If not, drop the job.
- The jobs left in the calendar, in order, are your schedule.
The code in four languages
PHP
<?php
// jobs: [name, deadline (day number), profit]. One job per day, each takes one day.
function sequenceJobs(array $jobs): array {
usort($jobs, fn($a, $b) => $b[2] <=> $a[2]); // biggest profit first
$maxDay = max(array_column($jobs, 1));
$slots = array_fill(0, $maxDay, null); // one box per day
$profit = 0;
foreach ($jobs as [$name, $deadline, $p]) {
// try the latest free day at or before the deadline
for ($t = min($deadline, $maxDay); $t >= 1; $t--) {
if ($slots[$t - 1] === null) {
$slots[$t - 1] = $name;
$profit += $p;
break;
}
}
}
return [$slots, $profit];
}
[$slots, $profit] = sequenceJobs([['a', 2, 100], ['b', 1, 19], ['c', 2, 27], ['d', 1, 25], ['e', 3, 15]]);
echo implode(' ', $slots) . ' | profit ' . $profit; // c a e | profit 142JavaScript
// jobs: [name, deadline (day number), profit]. One job per day, each takes one day.
function sequenceJobs(jobs) {
jobs = [...jobs].sort((a, b) => b[2] - a[2]); // biggest profit first
const maxDay = Math.max(...jobs.map(j => j[1]));
const slots = new Array(maxDay).fill(null); // one box per day
let profit = 0;
for (const [name, deadline, p] of jobs) {
// try the latest free day at or before the deadline
for (let t = Math.min(deadline, maxDay); t >= 1; t--) {
if (slots[t - 1] === null) {
slots[t - 1] = name;
profit += p;
break;
}
}
}
return [slots, profit];
}
const [slots, profit] = sequenceJobs([['a', 2, 100], ['b', 1, 19], ['c', 2, 27], ['d', 1, 25], ['e', 3, 15]]);
console.log(slots.join(' ') + ' | profit ' + profit); // c a e | profit 142Java
import java.util.*;
public class Main {
record Job(String name, int deadline, int profit) {}
record Plan(String[] slots, int profit) {}
// One job per day, each takes one day.
static Plan sequenceJobs(List<Job> input) {
List<Job> jobs = new ArrayList<>(input);
jobs.sort((a, b) -> Integer.compare(b.profit(), a.profit())); // biggest profit first
int maxDay = 0;
for (Job j : jobs) maxDay = Math.max(maxDay, j.deadline());
String[] slots = new String[maxDay]; // one box per day
int profit = 0;
for (Job job : jobs) {
// try the latest free day at or before the deadline
for (int t = Math.min(job.deadline(), maxDay); t >= 1; t--) {
if (slots[t - 1] == null) {
slots[t - 1] = job.name();
profit += job.profit();
break;
}
}
}
return new Plan(slots, profit);
}
public static void main(String[] args) {
Plan plan = sequenceJobs(List.of(new Job("a", 2, 100), new Job("b", 1, 19),
new Job("c", 2, 27), new Job("d", 1, 25), new Job("e", 3, 15)));
System.out.println(String.join(" ", plan.slots()) + " | profit " + plan.profit()); // c a e | profit 142
}
}Go
package main
import (
"cmp"
"fmt"
"slices"
"strings"
)
type Job struct {
name string
deadline int // day number
profit int
}
// sequenceJobs: one job per day, each takes one day. Returns the day boxes and the total profit.
func sequenceJobs(input []Job) ([]string, int) {
jobs := slices.Clone(input)
slices.SortFunc(jobs, func(a, b Job) int { return cmp.Compare(b.profit, a.profit) }) // biggest profit first
maxDay := 0
for _, j := range jobs {
maxDay = max(maxDay, j.deadline)
}
slots := make([]string, maxDay) // one box per day ("" = empty)
profit := 0
for _, job := range jobs {
// try the latest free day at or before the deadline
for t := min(job.deadline, maxDay); t >= 1; t-- {
if slots[t-1] == "" {
slots[t-1] = job.name
profit += job.profit
break
}
}
}
return slots, profit
}
func main() {
slots, profit := sequenceJobs([]Job{{"a", 2, 100}, {"b", 1, 19}, {"c", 2, 27}, {"d", 1, 25}, {"e", 3, 15}})
fmt.Println(strings.Join(slots, " ")+" | profit", profit) // c a e | profit 142
}Time: O(n log n + n × d) · Extra space: O(d). Sorting plus, for each job, a short walk back through at most d day-slots (d = largest deadline). A smarter slot finder can speed this up.
Watch out for
- Placing each job on the earliest free day. That wastes early days that tight-deadline jobs need.
- Sorting by deadline instead of by profit for this version of the problem.
- Forgetting that deadlines larger than the number of jobs just mean "any day", so cap the search at the calendar size.
Quick check
- In what order do we consider jobs?
- By deadline, earliest first
- In the order given
- By profit, highest first
- Why place a job on the latest free day before its deadline?
- It keeps earlier days open for jobs with tight deadlines
- It looks neat
- It makes sorting faster
- A job finds all days up to its deadline full. What happens?
- It pushes another job out
- It is skipped
- It goes after the deadline