Predict the output
What does this print?
orders, goal = [2, 3, 1, 2, 4, 3], 7
best, total, left = 0, 0, 0
for right, x in enumerate(orders):
total += x
while total >= goal:
size = right - left + 1
best = size if best == 0 else min(best, size)
total -= orders[left]
left += 1
print(best)Choose an answer, then open Show answer below to check it.
Show answer
Answer: A 2
Right: [4, 3] reaches 7 with two orders.
The shrink loop keeps trimming while the goal is still met, which uncovers the two-order run at the end.
Why not the others?
- B 3. [2, 4, 3] is not the shortest; [4, 3] works too.
- C 4. [2, 3, 1, 2] reaches 8, but shorter runs exist later.