835. Best Job Schedule
A freelance editor is offered several contracts. Contract i runs from time start[i] to time end[i] and pays profit[i]. The editor can work on one contract at a time, but may begin a contract at the very moment another one finishes. A contract whose end[i] is not greater than start[i] was cancelled and cannot be taken.
Return the largest total pay obtainable from a set of contracts that never overlap, or 0 if none can be taken. The three arrays have the same length. Aim for time O(n log n) and space O(n).
Example 1
- Input:
- start = [2,5,9,11]end = [6,9,14,13]profit = [30,25,40,12]
- Output:
- 70
- Explanation:
The jobs 2-6 and 9-14 do not overlap and pay 30 + 40; every other combination pays less, so the best total is
70.
Example 2
- Input:
- start = [3,3], end = [3,8], profit = [100,5]
- Output:
- 5
- Explanation:
The first job ends when it starts (zero length), so it is cancelled; only the second pays, giving
5.
Example 3
- Input:
- start = [1,4,7,10]end = [4,7,10,13]profit = [9,9,9,9]
- Output:
- 36
- Explanation:
Each job starts exactly when the previous one ends, so all four fit:
36.
Constraints
1 ≤ n ≤ 5 * 104, where n is the common length of the three arrays
0 ≤ start[i], end[i] ≤ 109
1 ≤ profit[i] ≤ 104
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
- Time per case
- Python 2,000 msC++ 500 msJava 1,000 msJavaScript 1,000 msTypeScript 1,000 ms
Expected complexity
- Time
- O(n log n)
- Space
- O(n)