The longest quiet period
transcript
A chat app sends a daily digest. It wants the quietest long stretch. Quiet means at most k events.
First count the events in each second. Each second gets one number. The whole day is one array.
Ten seconds, with a count for each. Here k is two. Seconds six to nine hold two events.
Add the second on the right. If the window has too many events, move the left end forward.
Take the stretch from second six. Take it up to second nine. Both ends count, so add one.
Both ends of the window are included. So the length is right minus left plus one. Without it, every answer is one short.
In the interview, ask if seconds or events. Ask if k is inclusive. And ask how long the day is.
Each second joins the window once. And leaves the window once. So the cost is linear in the day.
Count for each second. Length is right minus left plus one. Full breakdown in the lecture.