Maximum Profit in Job Scheduling
Maximum Profit in Job Scheduling is LeetCode 1235 (Hard). Job i runs from startTime[i] to endTime[i] and pays profit[i]. Pick jobs that do not overlap and return the largest total profit.
- A job may start at the exact moment another one ends.
- There can be 50,000 jobs, so trying every subset (2ⁿ of them) is out. Times go up to 10⁹, so they cannot be used as array indexes either.
The target is O(n log n): one sort plus one binary search per job.
- 1 <= startTime.length == endTime.length == profit.length <= 5 * 10⁴
- 1 <= startTime[i] < endTime[i] <= 10⁹
- 1 <= profit[i] <= 10⁴
Intuition
This is the classic weighted job scheduling problem. Sort the jobs by end time and let dp[k] be the best profit you can earn using only the first k jobs. Each job then gives a two-way choice:
- Skip it: the best stays what the earlier jobs already earned.
- Take it: add its profit to the best plan built only from jobs that finish by its start time.
Because the list is sorted by end time, the jobs that finish by a given start time always form a prefix of the list. A binary search on the end times finds where that prefix stops, so each choice costs O(log n).
Intervals with weights, where you keep a set that never overlaps and maximise the total, is weighted interval scheduling: sort by end, then take or skip with a binary search. If every job had the same profit, the greedy earliest-end rule would be enough.
Approach
Before reading on, list the jobs by end time for start = [1, 2, 3, 3], end = [3, 4, 5, 6], profit = [50, 10, 40, 70]. For each job, which earlier jobs could run before it? Aim for O(n log n).
Sort the jobs by end time
Zip the three lists into (end, start, profit) triples and sort them. After sorting, every job that can run before job k sits somewhere to its left in the list.
Keep the end times in their own list
Copy the sorted end times into ends. This list is sorted, so it can be binary searched, and dp gets n + 1 slots with dp[0] = 0 for the plan that uses no jobs.
Find how many earlier jobs fit
For job k with start s, prev = bisect_right(ends, s, 0, k) counts the earlier jobs that end at or before s. Those prev jobs are exactly the ones that can sit before job k in a plan.
Take the job or skip it
Set dp[k + 1] = max(dp[k], profit + dp[prev]). The first value skips the job and keeps the best plan of the first k jobs; the second takes it on top of the best plan that finishes in time.
Why the prefix is all you need
Every job among the first prev ends by s, so any of them can come before job k. Every other earlier job ends after s but no later than job k, so its final stretch lies inside job k's time and they overlap. So dp[prev] is the best plan that fits.
Return the last value
dp[n] is the best plan that may use any of the n jobs, so it is the answer.
Code in Python, C++ and Java
Common pitfalls
Treating jobs that touch as overlapping
prev = bisect_left(ends, s, 0, k)
prev = bisect_right(ends, s, 0, k)
bisect_left stops before any job that ends exactly at s, so back-to-back jobs are never combined. With start = [1, 3], end = [3, 5], profit = [20, 20] it returns 20 instead of 40.
Picking jobs greedily by end time
for e, s, p in jobs:
if s >= last_end:
total += p
last_end = eprev = bisect_right(ends, s, 0, k) dp[k + 1] = max(dp[k], p + dp[prev])
Earliest end first is right when every job is worth the same, but profits break it. With start = [1, 2], end = [3, 5], profit = [1, 100] the greedy takes the first job and is then blocked, returning 1 instead of 100.
Always taking the current job
dp[k + 1] = p + dp[prev]
dp[k + 1] = max(dp[k], p + dp[prev])
Without the skip option, dp[k + 1] can be smaller than dp[k]. With start = [1, 2], end = [3, 4], profit = [50, 10] the last value is 10, although taking only the first job earns 50.
Edge cases
start = [1, 1, 1], end = [2, 3, 4], profit = [5, 6, 4]Each binary search returns 0, so taking a job always means taking it alone. dp keeps the largest single profit, 6.
Complexity
ends and dp each hold n entries.Three job problems that look alike
What each job carries decides the method.
| Problem | Each job has | Goal | Method |
|---|---|---|---|
| Maximum Profit in Job Scheduling | fixed start, end and profit | largest total profit | sort by end, take-or-skip DP, binary search |
| N meetings in one room | fixed start and end | most jobs | greedy: earliest end first |
| Job sequencing with deadlines | deadline, one time unit, profit | largest profit by the deadlines | greedy: highest profit into the latest free slot |
Maximum Profit in Job Scheduling FAQ
Can I sort by start time instead?
Yes, and many solutions do. Sort by start, fill dp from the right so dp[i] is the best profit from job i onward, and binary search the start times for the first job that starts at or after job i ends. The cost is the same; end-time order just reads left to right along the timeline.
Is there a version with a heap instead of binary search?
Sort by start time and keep a min-heap of (end, total) for plans still running. Before each job, pop every plan that ends by its start and keep the best popped total; then push (end, best + profit). The answer is the best total after draining the heap. It is also O(n log n).
How do I list which jobs were chosen?
Store prev for every job. Walk back from k = n: if dp[k] equals dp[k - 1], job k - 1 was skipped, so step to k - 1; otherwise it was taken, so record it and jump to its prev.