← Questions

Best k-day run in a cyclic sprint schedule

0
AmazonCoding

A training program is made of n back-to-back sprints. Sprint i lasts days[i] days. On day j of a sprint (counting from 1 within that sprint), finishing that day's work earns j points. So a 4-day sprint is worth 1, 2, 3, 4 on its four days.

The program is cyclic: when the last sprint ends, sprint 0 starts again, and this repeats forever.

You will train for exactly k consecutive days. You may start on any day of any sprint, and the run may wrap from the last sprint into the next loop. Return the maximum total points you can earn.

Implement:

def max_sprint_points(days: list[int], k: int) -> int: ...

Constraints

  • 1 <= n <= 10^5
  • 1 <= days[i] <= 10^9
  • 1 <= k < sum(days)
  • Sprint lengths can be large, so a solution that walks day by day is too slow.

Example 1

days = [3, 1, 4], k = 5. One loop earns 1 2 3 | 1 | 1 2 3 4. The best run of 5 days takes the last two days of sprint 2 (3, 4) and then all of sprint 0 in the next loop (1, 2, 3), for 13.

Example 2

days = [2, 5], k = 3. The best run is the last three days of the 5-day sprint, 3 + 4 + 5 = 12.

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding