All problems
0323HardArrayDynamic Programming

Cheapest Shift Plan For A Restoration Queue

Tracked in this browser only
Write code

Trains the technique from

LeetCode 1335Minimum Difficulty of a Job Schedule

This is an original problem, written from a brief that listed the technique, the difficulty, the topics, the function shape and the input bounds — none of that problem's wording, examples, hints or editorials. The link is there so you can map your practice onto the standard set.

Same function shape, different story and different numbers.

A film archive restores reels in a fixed queue order. jobDifficulty[i] is the wear rating of the reel at position i, and the queue must be handled in exactly d shifts.

A shift takes a contiguous run of reels from the front of what is left, and every shift has to take at least one reel. The technician on a shift is billed for the single worst reel they touched, so a shift costs the largest wear rating inside its run. The bill for the whole plan is the sum over the d shifts.

Return the smallest possible bill. If the queue cannot be cut into d non-empty runs at all, return -1.

Examples

Example 1

Input
jobDifficulty = [4, 1, 9, 2], d = 2
Output
11

The plan [4, 1, 9] then [2] uses two contiguous non-empty runs and is billed 9 + 2 = 11.

Example 2

Input
jobDifficulty = [7, 3, 5, 8, 2], d = 3
Output
17

The plan [7], [3, 5, 8], [2] is billed 7 + 8 + 2 = 17, and each of its three runs is non-empty and contiguous.

Example 3

Input
jobDifficulty = [6, 2], d = 3
Output
-1

Only two reels are queued, so three non-empty runs cannot be formed and the sentinel is returned.

Example 4

Input
jobDifficulty = [5, 5, 5], d = 3
Output
15

Each shift must take exactly one reel and every reel is rated 5, so the bill is 5 + 5 + 5.

Constraints

  • 1 <= jobDifficulty.length <= 300
  • 0 <= jobDifficulty[i] <= 1000
  • 1 <= d <= 10

The signature

The editor is preloaded with this. It matches the parent problem's shape, so a solution that works here transfers to a judge unchanged.

Python
def cheapest_shift_plan(reels: list[int], shifts: int) -> int:
Java
public int cheapestShiftPlan(int[] reels, int shifts)
September 7
Apply