1253. Window Boost Count
A greenhouse has need.length plants in a row, and plant i must receive at least need[i] units of light. One lamp pass lights exactly k consecutive plants and gives each of them one unit; the window must lie fully inside the row, and passes may be repeated at the same place as often as you like. Every plant starts with zero light.
Return the minimum number of lamp passes after which every plant i has at least need[i] units, as a 64-bit integer. Sweep left to right with a running coverage count and a difference array; whenever plant i is short, add the missing number of passes in the window starting at min(i, n - k). Aim for O(n) time and O(n) space.
Example 1
- Input:
- need = [4,0,6,2,5,1], k = 3
- Output:
- 9
- Explanation:
Passes of width 3 are needed as 4 at start 0, 2 at start 2 and 3 at start 3, so the minimum is 9.
Example 2
- Input:
- need = [0,3,0,0,2,4,1], k = 2
- Output:
- 7
- Explanation:
Passes of width 2 are needed as 3 at start 1, 2 at start 4 and 2 at start 5, so the minimum is 7.
Example 3
- Input:
- need = [5,2,3,4], k = 2
- Output:
- 9
- Explanation:
Passes of width 2 are needed as 5 at start 0, then 3 at start 2 and 1 more at start 2 for the last plant, so the minimum is 9.
Constraints
1 ≤ k ≤ n ≤ 2*105 where n = need.length; 0 ≤ need[i] ≤ 109; the answer fits in a 64-bit integer.
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)