GeeksforGeeks Medium

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 most n time slots.

With up to 10⁵ jobs, the solution should run in about O(n log n).

Constraints
  • 1 <= deadline.size() = profit.size() <= 10⁵
  • 1 <= deadline[i] <= deadline.size()
  • 1 <= profit[i] <= 500
  • Each job takes exactly one unit of time
greedysortingscheduling
Open on GeeksforGeeks ↗
02

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.

How to spot this pattern

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.

03

Approach

Try it first

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).

1

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.

2

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.

3

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.

4

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.

5

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.

04

Code in Python, C++ and Java

▶1class Solution:
▶2 def jobSequencing(self, deadline, profit):
▶3 n = len(deadline)
▶4 jobs = sorted(zip(profit, deadline), reverse=True)
▶5 free = list(range(n + 1))
▶6 
▶7 def latest_free(t):
▶8 while free[t] != t:
▶9 free[t] = free[free[t]]
▶10 t = free[t]
▶11 return t
▶12 
▶13 count = total = 0
▶14 for p, d in jobs:
▶15 t = latest_free(d)
▶16 if t == 0:
▶17 continue
▶18 free[t] = t - 1
▶19 count += 1
▶20 total += p
▶21 return [count, total]
jobs, richest first40by 130by 120by 410by 1time slotsfreetime 1freetime 2freetime 3freetime 4profit0sort jobs by profit, richest first
jobs4
slots4times 1 to 4
Every job takes one slot, so a slot should go to the richest job that can still use it. The jobs are lined up by profit, and each card shows the latest time it may run (by its deadline). There are 4 one-unit slots, all free.
jobs, richest first40by 130by 120by 410by 1time slots40time 1deadlinefreetime 2freetime 3freetime 4profit4040 due by 1 → slot 1 is free, take it
job40deadline 1
slot1its deadline
profit40
The job worth 40 goes in slot 1, the latest it may use. Taking the last allowed slot leaves every earlier slot open for jobs with tighter deadlines.
jobs, richest first40by 130by 120by 410by 1time slots40time 1deadlinefreetime 2freetime 3freetime 4profit4030 due by 1: slot 1 is full → skip it
job30deadline 1
latest freenonetime 1 or earlier
The job worth 30 must run at time 1 or earlier, and every one of those slots already holds a richer job. Taking it would mean dropping one of them for less money, so it is skipped.
jobs, richest first40by 130by 120by 410by 1time slots40time 1freetime 2freetime 320time 4deadlineprofit40+20=6020 due by 4 → slot 4 is free, take it
job20deadline 4
slot4its deadline
profit60
The job worth 20 goes in slot 4, the latest it may use. Taking the last allowed slot leaves every earlier slot open for jobs with tighter deadlines.
jobs, richest first40by 130by 120by 410by 1time slots40time 1deadlinefreetime 2freetime 320time 4profit40+20=6010 due by 1: slot 1 is full → skip it
job10deadline 1
latest freenonetime 1 or earlier
The job worth 10 must run at time 1 or earlier, and every one of those slots already holds a richer job. Taking it would mean dropping one of them for less money, so it is skipped.
jobs, richest first40by 130by 120by 410by 1time slots40time 1freetime 2freetime 320time 4profit40+20=60answer: 2 jobs, profit 60
count2
profit60
Every job has been placed or skipped. 2 slots are filled, worth 60, so the answer is [2, 60]. The empty slots stay empty: no skipped job was allowed to run that late.
05

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.

▶1import heapq
▶2 
▶3class Solution:
▶4 def jobSequencing(self, deadline, profit):
▶5 heap = []
▶6 for d, p in sorted(zip(deadline, profit)):
▶7 if len(heap) < d:
▶8 heapq.heappush(heap, p)
▶9 elif heap[0] < p:
▶10 heapq.heapreplace(heap, p)
▶11 return [len(heap), sum(heap)]
06

Common pitfalls

Sorting by deadline instead of profit

✗ Wrong
jobs = sorted(zip(profit, deadline), key=lambda j: j[1])
✓ Right
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

✗ Wrong
t = 1
while t <= d and taken[t]:
    t += 1
✓ Right
t = 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

✗ Wrong
t = d
while t > 0 and taken[t]:
    t -= 1
✓ Right
t = 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.

07

Complexity

Time
O(n log n)
Space
O(n)
Sorting costs O(n log n). With the chain shortening, each slot lookup is O(log n) at worst and close to O(1) in practice. The sorted pairs and the free array take O(n).
08

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.

ProblemJobsGoalMethod
Job sequencing (this page)one unit long, can run in any slot up to the deadlinemost profitprofit order, latest free slot
N meetings in one roomfixed start and end timesmost meetingssort by end time, take each meeting that starts after the last one ends
Maximum profit in job schedulingfixed start and end times, each with a profitmost profitsort by end time, dynamic programming with binary search
09

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.