Skip to contentKEEP THE CRAFTTHE SANCTUARY

Shrink Until the Window Is Valid

Grow the window on the right, shrink it from the left while the sum is too big, and keep the longest one.

The problem

Implement longest_bounded_window(values, limit). values holds non-negative integers and limit >= 0. Find the longest block of consecutive values whose sum is at most limit, and return it as [start, end], meaning values[start:end]. If several blocks share the longest length, return the one that starts first. If no single value fits, return [0, 0]. For example, [3, 1, 1, 1, 4] with limit 3 gives [1, 4].

Starter code

def longest_bounded_window(values, limit):
    pass

Public examples

  1. values = [2, 1, 2, 1, 1], limit = 4gives[1, 4]
  2. values = [2, 2, 2], limit = 4gives[0, 2]

Solving it in the practice book needs an active paid plan.

Open it in the practice book
One thought at a time.