Sort by close, not by open
transcript
Batch runs happen at allowed minutes. Each window needs one run inside it. Sort the windows. By what?
The window that closes first needs a run soon. Take the latest allowed minute before it closes. That run also helps the later windows.
Take windows from one to ten, and two to three. Sorted by open, the first run lands at ten. The code credits it to the window that closed at three. Sorted by close, that window comes first. Now the answer is right.
Allow only the minute ten. Sorted by open, the answer is one. Sorted by close, it is minus one. No run falls inside the second window.
A run is safe if it is not after the close. Sorting by close guarantees that. Sorting by open does not. That one wrong credit hides an impossible window. Sorting by close exposes it at once.
Close first, then the latest allowed run. Never sort these by open. Full breakdown in the lecture.