Practice › Dynamic Programming
Maximum Profit in Job Scheduling
Hard · Dynamic Programming · Asked at Amazon, Google, Microsoft, Airbnb
Job i runs from start[i] to end[i] and pays profit[i]. You can do jobs that do not overlap — a job ending at time t may be followed by a job starting at time t. Print the maximum total profit.
Input
First line: n. Next n lines: start end profit.
Output
The maximum profit.
Constraints
- 1 ≤ n ≤ 5 × 10^4
- 1 ≤ start < end ≤ 10^9
- 1 ≤ profit ≤ 10^4
Sort jobs by end time. dp[i] = best profit using the first i jobs = max(dp[i − 1], profit[i] + dp[j]), where j counts the jobs ending at or before start[i] — found by binary search over the sorted end times.
Example 1
Input
4 1 3 50 2 4 10 3 5 40 3 6 70
Output
120
Example 2
Input
5 1 3 20 2 5 20 3 10 100 4 6 70 6 9 60
Output
150
Hints
- Sort the jobs by end time.
- For job i: either skip it, or take it plus the best schedule of jobs that end by its start.
- Find "jobs that end by its start" with a binary search over the sorted end times.
Approach
After sorting by end, dp[i] = best profit using the first i jobs. For job i (1-based) with start s and profit p, let j = the number of jobs whose end ≤ s (upper bound in the end-time array, searched within the first i − 1 jobs). Then dp[i] = max(dp[i − 1], dp[j] + p). The answer is dp[n]. Profits sum to at most 5 × 10^8, but 64-bit is a safe habit.
Complexity: O(n log n) time · O(n) space
Solution
C++ solution
#include <bits/stdc++.h>
using namespace std;
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<array<long long, 3>> jobs(n); // end, start, profit
for (auto &j : jobs) cin >> j[1] >> j[0] >> j[2];
sort(jobs.begin(), jobs.end());
vector<long long> ends(n), dp(n + 1, 0);
for (int i = 0; i < n; i++) ends[i] = jobs[i][0];
for (int i = 1; i <= n; i++) {
long long s = jobs[i - 1][1], p = jobs[i - 1][2];
int j = upper_bound(ends.begin(), ends.begin() + i - 1, s) - ends.begin();
dp[i] = max(dp[i - 1], dp[j] + p);
}
cout << dp[n] << "\n";
return 0;
}Python solution
import sys
from bisect import bisect_right
data = list(map(int, sys.stdin.read().split()))
n = data[0]
jobs = sorted(((data[2 + 3 * i], data[1 + 3 * i], data[3 + 3 * i]) for i in range(n)))
ends = [e for e, _, _ in jobs]
dp = [0] * (n + 1)
for i in range(1, n + 1):
e, s, p = jobs[i - 1]
j = bisect_right(ends, s, 0, i - 1)
dp[i] = max(dp[i - 1], dp[j] + p)
print(dp[n])Java solution
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
StreamTokenizer in = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
in.nextToken();
int n = (int) in.nval;
long[][] jobs = new long[n][3]; // start, end, profit
for (int i = 0; i < n; i++)
for (int k = 0; k < 3; k++) {
in.nextToken();
jobs[i][k] = (long) in.nval;
}
Arrays.sort(jobs, (a, b) -> Long.compare(a[1], b[1]));
long[] dp = new long[n + 1];
for (int i = 1; i <= n; i++) {
long s = jobs[i - 1][0];
int lo = 0, hi = i - 1;
while (lo < hi) {
int m = (lo + hi) / 2;
if (jobs[m][1] <= s) lo = m + 1;
else hi = m;
}
dp[i] = Math.max(dp[i - 1], dp[lo] + jobs[i - 1][2]);
}
System.out.println(dp[n]);
}
}Solve Maximum Profit in Job Scheduling online · All practice problems