Count exactly K. Subtract.
transcript
How many sessions touch exactly two categories? One window cannot count it. Two windows can.
Eight plays, as category ids. Music, podcasts, audiobooks. Look at the sessions that end at index five. Starts two, three and four touch exactly two categories. But index five alone is only music. One category. So the good starts have two edges. One window has one.
Every session with at most two categories splits in two. Either it has exactly two. Or it has at most one. So subtract the second group from the whole.
Counting at most K is one window. Grow on the right. Shrink while it holds too many. Every start from left to right is valid. So add right minus left plus one. Long logs pass two billion. Use sixty-four bits.
Twenty-two sessions with at most two categories. Nine sessions with at most one. Twenty-two minus nine is thirteen. Exactly two.
Two easy counts. One subtraction. Both passes are order n.
Full breakdown in the lecture.