Peak k-hour draw: hints, no solution

Easy · Sliding window

A smart meter reports the electricity draw of a building for each hour of a day or a week, in kilowatt-hours. A reading can be negative when rooftop panels feed power back into the grid.

The operator wants the busiest stretch of exactly k consecutive hours, the one whose readings add up to the largest total. Return the starting hour of that stretch, counting hours from 0. If several stretches share the largest total, return the earliest start. It is guaranteed that k is at least 1 and no larger than the number of readings.

Examples

Input
draw = [3, 1, 4, 1, 5, 9, 2, 6], k = 3
Output
5
Note
The last three readings 9, 2 and 6 total 17, which is the most, starting at hour 5.
Input
draw = [5, 5, 5, 5], k = 2
Output
0
Note
All windows tie at 10, so the earliest start 0 wins.
Input
draw = [-3, -1, -4, -2], k = 1
Output
1
Note
With k = 1 the largest single reading, -1, sits at hour 1.

Constraints

  • 1 <= k <= draw.length <= 300000
  • -500 <= draw[i] <= 500
  • Return the earliest start when totals tie

Hint 1: a nudge

When you move from one stretch to the next one hour later, how much of the total really changes?

Unlock the full hints and timed practice with a pass

The second and third hints and the timed, runner-checked practice are part of a pass.

See pricing

Sign in

Check my resume first