call(back)
Algorithms & Data StructuresmediumSign in to star problems

Count Pins on a Two-Column Screen

The home feed is two columns of pins. Each pin is (top, bottom, column) with top < bottom and column "L" or "R"; pins in the same column never overlap. The screen shows a vertical window of length screenLen, scrolled to any offset. Return the maximum number of pins fully visible at once.

pins = [(1,4,"L"), (2,3,"R"), (4,8,"R"), (6,9,"L")], screenLen = 5  =>  2
window [1,6] shows (1,4,L) and (2,3,R); window [4,9] shows (4,8,R) and (6,9,L)

Up to 100,000 pins; coordinates up to 10^9 and possibly fractional; input unsorted.

Hints

Hint 1

An optimal window can always be slid down until its top edge touches some pin's top without losing a fully visible pin — so only the n pin tops matter as candidate offsets.

Hint 2

Within one column, non-overlapping pins sorted by top are also sorted by bottom, so the fully visible ones for window [y, y + L] form one contiguous run.

Hint 3

Binary search each column: first pin with top >= y, last pin with bottom <= y + L. The run length is the count.

Asked at