Bakery Oven Run Windows

Choose fixed-count spaced intervals with maximum total value

Hard2-D Dynamic ProgrammingMonotonic QueueReconstruction

A bakery has divided tomorrow's oven schedule into time slots. The integer values[i] is the net benefit of operating the oven during slot i; it can be negative.

Choose exactly runs nonempty oven runs. Each run occupies one contiguous interval of slots, and its length must be between min_length and max_length, inclusive. Between consecutive runs, at least cooldown slots must remain unused. There is no required cooldown before the first run or after the last run.

The schedule's total benefit is the sum of values[i] over all occupied slots. Unused slots contribute nothing. Runs remain distinct even when cooldown is zero and two selected intervals are adjacent.

Return the selected intervals as [start, end] pairs using zero-based, inclusive endpoints, in chronological order. Maximize total benefit. If several schedules have the same maximum benefit, choose the one whose flattened endpoint list [start1, end1, start2, end2, ...] is lexicographically smallest.

The input guarantees that at least one valid schedule exists.

Examples

Example 1

Input: values = [5,4,-10,6,3], runs = 2, min_length = 1, max_length = 2, cooldown = 1
Output: [[0,1],[3,4]]

Two short operating periods can collect the positive benefits on opposite sides of the expensive middle slot. That middle slot also supplies the required cooldown.

Example 2

Input: values = [-4,-1,-8,-2,-3], runs = 2, min_length = 2, max_length = 3, cooldown = 1
Output: [[0,1],[3,4]]

All benefits are negative, but exactly two runs are still required. Each run must occupy two slots, and the unused slot between them is mandatory.

Example 3

Input: values = [0,0,0,0], runs = 2, min_length = 1, max_length = 3, cooldown = 0
Output: [[0,0],[1,1]]

Every valid schedule has the same total benefit. The endpoint tie-break first chooses the earliest possible start, then the earliest possible end, and continues in that order.

Constraints

  • 1 <= values.length <= 100000
  • -1000000000 <= values[i] <= 1000000000
  • 1 <= runs <= 20
  • 1 <= min_length <= max_length <= values.length
  • 0 <= cooldown <= values.length
  • runs * min_length + (runs - 1) * cooldown <= values.length

An intended solution takes O(runs * values.length) time and O(runs * values.length) space when reconstructing the canonical schedule. Totals may exceed 32-bit integer range, but remain exactly representable by JavaScript Number.

Hints

Show hint 1

Let dp[t][i] be the best benefit obtainable by placing exactly t runs in the suffix starting at i. Separate skipping slot i from starting a run there.

Show hint 2

After introducing prefix sums, the best endpoint for a run starting at i is a maximum over a moving endpoint range. Maintain that maximum with a monotonic deque, retaining the smallest endpoint on ties.

Follow-up questions

What an interviewer might ask once you have a working solution.

  • If only the maximum total benefit is needed, how can auxiliary space be reduced to O(n)?
  • How would the transition change if the minimum cooldown after a run depended on that run's position in the schedule?

Practice this with an AI interviewer

Explain your approach out loud, write Python or JavaScript, run it against hidden tests (including large inputs), and get a scored debrief.

Start this problem