Weighted Job Scheduling
Pattern: LIS family
Sort jobs by end time. best[i] is the most profit from the first i jobs: skip job i, or take it plus best of the jobs ending by its start, found by binary search.
The problem
Given startTime, endTime and profit for each job, return the most total profit from jobs that do not overlap. A job ending at time t is compatible with one starting at t.
Think first
- Why sort by end time?
- If you take job i, which jobs can come before it?
- How do you find them fast?
Hints
Brute force, and why it fails
Try every subset of jobs: 2ⁿ.
The key insight
In end-time order, the jobs compatible with job i are a prefix, so each choice is skip or take-after-that-prefix.
Dry run
Jobs (1-3, 50), (2-4, 10), (3-5, 40), (3-6, 70), sorted by end.
Step 1 of 2
The plan
sort by end
best[0] = 0
for i: k = number of jobs ending <= start[i]
best[i] = max(best[i - 1], best[k] + profit[i])
Complexity
- Time
O(n log n)- Space
O(n)
Sorting plus one binary search per job.
Edge cases
- One job.
- All overlapping: the most profitable one.
- Touching jobs.
I've built my logic, show the code
Java
import java.util.*;
class Solution {
public int jobScheduling(int[] startTime, int[] endTime, int[] profit) {
int n = startTime.length;
Integer[] order = new Integer[n];
for (int i = 0; i < n; i++) order[i] = i;
Arrays.sort(order, (a, b) -> Integer.compare(endTime[a], endTime[b])); // by end time
int[] ends = new int[n], best = new int[n + 1]; // best[i]: the most profit from the first i jobs in that order
for (int i = 0; i < n; i++) ends[i] = endTime[order[i]];
for (int i = 1; i <= n; i++) {
int job = order[i - 1], lo = 0, hi = i - 1; // how many earlier jobs end by this one's start?
while (lo < hi) {
int mid = (lo + hi + 1) / 2;
if (ends[mid - 1] <= startTime[job]) lo = mid; else hi = mid - 1;
}
best[i] = Math.max(best[i - 1], best[lo] + profit[job]); // skip it, or take it after the compatible ones
}
return best[n];
}
}C++
class Solution {
public:
int jobScheduling(vector<int>& startTime, vector<int>& endTime, vector<int>& profit) {
int n = static_cast<int>(startTime.size());
vector<int> order(n);
for (int i = 0; i < n; i++) order[i] = i;
sort(order.begin(), order.end(), [&](int a, int b) { return endTime[a] < endTime[b]; }); // by end time
vector<int> ends(n), best(n + 1, 0); // best[i]: the most profit from the first i jobs in that order
for (int i = 0; i < n; i++) ends[i] = endTime[order[i]];
for (int i = 1; i <= n; i++) {
int job = order[i - 1];
int k = static_cast<int>(upper_bound(ends.begin(), ends.begin() + (i - 1), startTime[job]) - ends.begin()); // compatible jobs
best[i] = max(best[i - 1], best[k] + profit[job]); // skip it, or take it after the compatible ones
}
return best[n];
}
};Python
import bisect
class Solution:
def jobScheduling(self, startTime: list[int], endTime: list[int], profit: list[int]) -> int:
jobs = sorted(zip(startTime, endTime, profit), key=lambda j: j[1]) # by end time
ends = [e for _, e, _ in jobs]
best = [0] * (len(jobs) + 1) # best[i]: the most profit from the first i jobs
for i, (s, e, p) in enumerate(jobs, 1):
k = bisect.bisect_right(ends, s, 0, i - 1) # earlier jobs that end by this start
best[i] = max(best[i - 1], best[k] + p) # skip it, or take it after the compatible ones
return best[-1]Common mistakes
- Greedy by profit.
- Treating touching jobs as overlapping.