1719. No Long Shifts
A bakery has n bakers standing in a row, and baker i would produce output[i] loaves if called in today. A safety rule says that no more than k bakers standing next to each other may all be on duty at the same time, so every run of consecutive working bakers has length at most k. Choose which bakers work so that the total number of loaves is as large as possible, and return that maximum as a 64-bit integer. Everyone not chosen produces nothing. The row has 1 to 200000 bakers, 1 <= k <= n and each output lies between 0 and 10^9. Aim for O(n) time and O(n) space: equivalently drop a cheapest set of bakers so that every k + 1 consecutive bakers contain a dropped one, and keep dp values in a sliding-window minimum deque.
Example 1
- Input:
- output = [5,1,5], k = 1
- Output:
- 10
- Explanation:
With k = 1 no two neighbours may both work, so choose the two 5s for a total of 10.
Example 2
- Input:
- output = [3,1,4,1,5], k = 2
- Output:
- 12
- Explanation:
Dropping the two bakers with output 1 splits the row into runs of length 1, for a total of 14 - 2 = 12.
Example 3
- Input:
- output = [4,4,4], k = 3
- Output:
- 12
- Explanation:
Since k equals the row length, all three can work for 12 loaves.
Constraints
1 ≤ k ≤ n ≤ 2 * 105
0 ≤ output[i] ≤ 109
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 4,000 msC++ 1,000 msJava 2,000 msJavaScript 2,000 msTypeScript 2,000 ms
Expected complexity
- Time
- O(n)
- Space
- O(n)