1765. Deadline Knapsack
A freelance translator has n offers. Offer i takes durations[i] hours of uninterrupted work, must be finished no later than hour deadlines[i] (counted from now), and pays profits[i]. She works alone, on one offer at a time, starting at hour 0, and cannot pause an offer once started. She may decline any offer; accepted offers must all meet their deadlines.
Return the maximum total payment she can collect. The answer can exceed 32 bits, so return a long. Hint: among any accepted set that can be scheduled at all, working in order of deadline also works, which lets you process offers sorted by deadline with a table over the finishing hour. With T the largest deadline, aim for O(n log n + n * T) time and O(T) space.
Example 1
- Input:
- durations = [3,2,2]deadlines = [4,5,5]profits = [10,7,5]
- Output:
- 17
- Explanation:
Take offer 0 (hours 0 to 3, due 4) and offer 1 (hours 3 to 5, due 5) for 10 + 7 = 17; adding offer 2 would finish at hour 7, past its deadline.
Example 2
- Input:
- durations = [5], deadlines = [4], profits = [100]
- Output:
- 0
- Explanation:
The only offer needs 5 hours but is due at hour 4, so nothing can be accepted.
Example 3
- Input:
- durations = [1,1,1]deadlines = [1,1,1]profits = [4,9,6]
- Output:
- 9
- Explanation:
All three offers are due at hour 1 and take an hour each, so only one fits; the best is the one paying 9.
Constraints
1 ≤ n ≤ 100, where n = durations.length = deadlines.length = profits.length
1 ≤ durations[i] ≤ 5000; 1 ≤ deadlines[i] ≤ 5000; 1 ≤ profits[i] ≤ 109
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 + n * T)
- Space
- O(T)