Busiest Stretch

EasySliding Window≈ 15 min

A site logs its visits per day in the list visits. Given a number of days k, find the k consecutive days with the most visits in total, and return that total.

Examples

text
visits = [3, 1, 4, 1, 5, 9, 2, 6], k = 3   →  17
visits = [5], k = 1                         →  5
visits = [0, 0, 7, 0], k = 2                →  7

In the first example the last three days, 9 + 2 + 6, add up to 17.

Constraints

  • Between 1 and 100,000 days, each with between 0 and 1,000 visits.
  • k is at least 1 and at most the number of days.
  • Aim for O(n): adding up every window from scratch costs n × k.
Sound off. Turn on for key clicks and a chime when the tests pass.Inline suggestions off. Recommended while you are learning. Turn on to complete words as you type.
Console
›Run to see what your code prints

Run checks the 3 visible cases here, in your browser. Submit runs all 6 on the judge, 3 of them hidden, and measures how your solution scales.