Drill

ProblemsPython › patterns

How often the load crossed the line

mediumpatternsSliding windowArraysPython

Capacity planning counts how many fixed-length stretches of the day carried at least a given amount of work.

windows_over_limit(load: list<int>, run_length: int, limit: int) → int

Solve it in the editor →

Where you start

def windows_over_limit(load: list[int], run_length: int, limit: int) -> int:
    

Worked examples

CallResult
windows_over_limit([1, 2, 3, 4, 5], 2, 5)3
windows_over_limit([1, 1, 1], 2, 10)0
windows_over_limit([5, 5, 5], 1, 5)3
windows_over_limit([1, 2], 3, 1)0

Hint

Slide one total across the list rather than re-summing each window, and test it at every stop.

Reference solution in Python
def windows_over_limit(load: list[int], run_length: int, limit: int) -> int:
    if run_length <= 0 or len(load) < run_length:
        return 0
    window = sum(load[:run_length])
    hits = 1 if window >= limit else 0
    for i in range(run_length, len(load)):
        window += load[i] - load[i - run_length]
        if window >= limit:
            hits += 1
    return hits

The same problem in another language

More patterns problems in Python