1738. Unit Job Penalties
A print shop has one machine that produces one sticker sheet per time slot. Order i becomes available at time release[i] and takes exactly one slot, so a sheet printed in slot [t, t + 1) is completed at time t + 1. Each completed order costs penalty[i] for every time unit it took, meaning penalty[i] * completion time in total, where completion time is measured from time 0. An order cannot start before its release, and the machine may idle.
Return the minimum possible sum of penalty[i] * completion[i] over all orders, as a long. When several orders are available, running one with the largest penalty is optimal; ties do not change the total. Expected: O(n log n) time, O(n) space.
Example 1
- Input:
- release = [0,0,0], penalty = [1,5,3]
- Output:
- 14
- Explanation:
Running penalties 5, 3, 1 in slots completing at times 1, 2, 3 costs 5 + 6 + 3 = 14.
Example 2
- Input:
- release = [0,2], penalty = [4,7]
- Output:
- 25
- Explanation:
The first order finishes at time 1 (cost 4), the machine idles, and the second finishes at time 3 (cost 21), for a total of 25.
Constraints
1 ≤ n ≤ 1000
0 ≤ release[i] ≤ 106
1 ≤ penalty[i] ≤ 1000
How this problem is judged
- Answers
- Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.
Expected complexity
- Time
- O(n log n)
- Space
- O(n)