Stage Sliding Spotlight
The theater's big spotlight lights exactly three seats at a time. Slide it along the row to find the loudest group of fans, without wasting time counting everyone again.
π§ Before we start
Oasis Dynamic Route remembered answers so it never repeated work. Today we reuse work again with a sliding window: a small frame over a list. When the frame moves one step, the program adds the seat that enters and removes the seat that leaves instead of recounting the whole frame.
Frame the spotlight
Each seat shows how loud its fan cheers. Tap the window ideas in the order a program uses them: the lit group, its total, then the move.
Slide with add and remove
The spotlight moves one seat right. Seat 4 enters (green) and seat 1 leaves (red dashed). Pick the update that gives the new total without recounting seats 2 and 3.
Find the loudest group
Slide the spotlight across the row. Each slide updates the total with add and remove. The scoreboard remembers the best total so far. Lock the spotlight when it sits on the loudest group.
Count the saved work
The theater grows to 12 seats and the spotlight to 5 seats. That makes 8 windows. Compare recounting each window with sliding, then choose the true comparison.
8 windows Γ 5 seats = ?
first 5 + 7 slides Γ 2 = ?
π What you learned
- A sliding window is a small frame that moves across a list.
- The first window is counted once to start the total.
- Each slide adds the entering item and removes the leaving item.
- Items that stay inside the window are never recounted.
- Keeping a best-so-far value lets one pass find the best group.
- With a window of 5 on 12 seats, sliding reads 19 seats instead of 40, and the saving grows with bigger lists.