Predict the output
This counts the inner-loop steps of a window. What does it print?
spend, budget = [3] * 10, 5
total = left = inner = 0
for right, x in enumerate(spend):
total += x
while total > budget:
total -= spend[left]
left += 1
inner += 1
print(inner, left)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 9 9
Right: each step after the first drops exactly one day, nine times in all.
Inner steps equal left-pointer moves, and the left pointer can move at most n times in total.
Why not the others?
- B 45 9. The inner loop does not rescan the window; it runs once per new day here.
- C 10 10. The left pointer stays one behind the right pointer, ending at 9.