1740. Unit Job Profits

Medium3.5 points estimatedGreedyHeapSorting

A print shop runs machines identical presses. Every order takes exactly one hour on a press, must be finished by the end of hour deadlines[i] (so it may be printed during any single hour from 1 to deadlines[i]), and pays profits[i] when printed. A press handles one order per hour, and orders you skip earn nothing.

Return the maximum total profit as a long. There are up to 100000 orders; aim for O(n log n) time and O(n) space by sorting orders by deadline and keeping the chosen profits in a min-heap, dropping the smallest whenever more than machines * deadline chosen orders would have to finish by that deadline.

Example 1

Input:
deadlines = [2,1,2,1]profits = [10,40,30,20]machines = 1
Output:
70
Explanation:

One press: at most one order can finish by hour 1 and two by hour 2, so print the 40 order in hour 1 and the 30 order in hour 2 for 70.

Example 2

Input:
deadlines = [1,1,1,3], profits = [5,6,7,8], machines = 2
Output:
21
Explanation:

Two presses allow two orders in hour 1, so keep 7 and 6 there, and the 8 order fits later, giving 21.

Example 3

Input:
deadlines = [3,3,3], profits = [4,9,2], machines = 1
Output:
15
Explanation:

Three hours are available before the shared deadline, so all three orders fit and the total is 15.

Constraints

1 ≤ n ≤ 105 (n = deadlines.length = profits.length)
1 ≤ deadlines[i] ≤ 105
1 ≤ profits[i] ≤ 109
1 ≤ machines ≤ 105

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 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms

Expected complexity

Time
O(n log n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…