Job Sequencing Problem
Job Sequencing Problem is a GFG problem (Medium). You get two arrays, deadline and profit, that describe n jobs. Every job takes one unit of time, only one job runs at a time, and job i pays profit[i] only if it finishes by time deadline[i].
- Return two numbers: how many jobs are done, and the largest total profit.
- Every deadline is between 1 and
n, so there are at mostntime slots.
With up to 10⁵ jobs, the solution should run in about O(n log n).
- 1 <= deadline.size() = profit.size() <= 10⁵
- 1 <= deadline[i] <= deadline.size()
- 1 <= profit[i] <= 500
- Each job takes exactly one unit of time
Intuition
The greedy method rests on two choices:
- Which job next: the one with the highest profit. Every job fills exactly one slot, so a slot is worth most when it goes to the richest job that can still use it.
- Which slot: the latest free slot at or before the job's deadline. A late slot can be used by few other jobs, while slot 1 can be used by every job, so early slots are kept for tight deadlines.
If no slot from the deadline down to 1 is free, the job is skipped. That is safe, because every slot it could use already holds a richer job.
Unit-length tasks, a deadline each, a reward each, and one worker: that is this problem. The same plan fits ad slots that expire, or exam questions that must be answered before a cutoff.
Approach
Before reading on, decide which job should be placed first, and which of its allowed slots it should take so it gets in the way of as few other jobs as possible. Aim for O(n log n).
Two ways to solve it
Take jobs from richest to poorest and give each the latest free slot by its deadline.
- Shows the schedule: slot
tholds the job that runs at timet. - Fast lookups: slot pointers skip full slots.
The standard greedy answer.
Take jobs by deadline and keep the accepted profits in a min-heap, swapping out the cheapest when a richer job arrives.
- No slot array: the heap size is the number of slots used.
- Gives totals only: the heap does not say which job runs when.
A common expected answer.
Both run in O(n log n). The slot method wins because it builds the actual schedule and follows the textbook greedy. The steps, code and live demo use it, and the heap code comes after the demo.
Sort the jobs by profit, highest first
Pair each profit with its deadline and sort by profit, largest first. Ties can go in any order: two jobs with equal profit add the same amount whichever is placed first.
Give each slot a pointer to the latest free slot
Slots run from 1 to n, and slot 0 means "nothing left". Keep an array free where free[t] starts as t. Following the pointers from t lands on the latest free slot at or before t, so a job jumps over full slots instead of checking them one by one.
Place the job or skip it
Look up the latest free slot t for the job's deadline. If t is 0, every slot the job could use is full, so skip it. Otherwise put the job in slot t, add its profit, and set free[t] = t - 1, so the next job that lands on t falls back one slot.
Shorten the pointer chains while looking up
During a lookup, point each slot passed two steps further down (free[t] = free[free[t]]). Long runs of full slots then collapse into short jumps, which keeps every lookup close to constant time.
Return the count and the total
After the last job, count is the number of filled slots and total is the sum of their profits. Return them as [count, total], with the count first.
Code in Python, C++ and Java
Deadline order, min-heap
Sort the jobs by deadline. The heap holds the profits of the jobs accepted so far, and its size is the number of slots used. A job with deadline d joins if fewer than d jobs are in the heap. Otherwise it replaces the cheapest accepted job, but only if it pays more. The heap ends holding exactly the chosen profits.
Common pitfalls
Sorting by deadline instead of profit
jobs = sorted(zip(profit, deadline), key=lambda j: j[1])
jobs = sorted(zip(profit, deadline), reverse=True)
For deadline = [1, 1], profit = [10, 40], deadline order places the 10 job in slot 1 first, and the 40 job then finds no slot. The code returns [1, 10] instead of [1, 40].
Taking the earliest free slot
t = 1
while t <= d and taken[t]:
t += 1t = latest_free(d)
For deadline = [2, 1], profit = [100, 50], the 100 job takes slot 1 even though slot 2 was allowed. The 50 job, which can only use slot 1, is then skipped: [1, 100] instead of [2, 150].
Walking back one slot at a time
t = d
while t > 0 and taken[t]:
t -= 1t = latest_free(d)
This gives the right answer but can be too slow. If all 10⁵ jobs have deadline 10⁵, the k-th job walks past k − 1 full slots, about 5 × 10⁹ checks in total.
Complexity
free array take O(n).Job sequencing vs other scheduling problems
These three are often confused because they all pick jobs for one worker. What moves, and what is counted, decides the method.
| Problem | Jobs | Goal | Method |
|---|---|---|---|
| Job sequencing (this page) | one unit long, can run in any slot up to the deadline | most profit | profit order, latest free slot |
| N meetings in one room | fixed start and end times | most meetings | sort by end time, take each meeting that starts after the last one ends |
| Maximum profit in job scheduling | fixed start and end times, each with a profit | most profit | sort by end time, dynamic programming with binary search |
Job Sequencing Problem FAQ
Is the job count also the most jobs any schedule can finish?
Yes. A job is skipped only when it cannot join the jobs already chosen, and later choices only add jobs, so it never could. The final set is one no job can be added to. For one-unit jobs with deadlines, all such sets have the same size, so the count is the largest possible as well.