1355. Weighted Finish Order
A single machine must run n jobs back to back starting at time 0, in an order you choose and with no gaps. Job i takes durations[i] time units and has an importance weight weights[i]. If job i finishes at time C_i, the cost of a schedule is the sum over all jobs of weights[i] * C_i. Return the smallest possible cost over all orders, as a 64-bit integer. Compare two jobs by the ratio durations / weights, running the job with the smaller ratio first, and test it by cross-multiplication rather than by dividing. Jobs with equal ratios may run in any order. The cost can reach about 10^14, so use 64-bit sums. Aim for O(n log n) time and O(n) space.
Example 1
- Input:
- durations = [3,1,2], weights = [1,1,1]
- Output:
- 10
- Explanation:
Running the jobs in order 1, 2, 3 finishes them at times 1, 3 and 6, so the cost is 1 + 3 + 6 = 10.
Example 2
- Input:
- durations = [4,2], weights = [1,9]
- Output:
- 24
- Explanation:
The second job has a tiny ratio 2/9, so it runs first, finishing at 2, and then the first job finishes at 6: 9*2 + 1*6 = 24.
Example 3
- Input:
- durations = [5], weights = [3]
- Output:
- 15
- Explanation:
With one job finishing at time 5 and weight 3, the cost is 15.
Constraints
1 ≤ n ≤ 105
durations.length == weights.length
1 ≤ durations[i], weights[i] ≤ 100
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)