839. Job Schedule by Day

Hard7.5 points estimatedArrayDynamic Programming3 hints

A workshop has a list jobs of repair tasks that must be finished in the given order, where jobs[i] is the effort rating of task i. The tasks are shared over exactly d working days: each day handles one contiguous block of the list and at least one task, and no task can be postponed out of order.

The strain of a day is the largest rating among the tasks done that day, and the total strain is the sum over all days. Return the minimum possible total strain, or -1 if there are fewer tasks than days.

Aim for time O(n * d) using a monotonic stack for the cheapest block end, and space O(n).

Example 1

Input:
jobs = [6,2,9,4,7,3], d = 3
Output:
17
Explanation:

Splitting into [6], [2] and [9,4,7,3] costs 6 + 2 + 9 = 17, which beats every other way to make three non-empty days.

Example 2

Input:
jobs = [8,1,1,8,2], d = 2
Output:
10
Explanation:

Putting the first four jobs on day one (hardest 8) and the last job on day two costs 8 + 2 = 10.

Constraints

1 ≤ jobs.length ≤ 104

0 ≤ jobs[i] ≤ 104

1 ≤ d ≤ 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 1,200 msC++ 300 msJava 600 msJavaScript 600 msTypeScript 600 ms

Expected complexity

Time
O(n * d)
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…