FunctionRacerProblemsRace

The Slot That Comes Next

MediumSorting and searchingPython, JavaScript, TypeScript, Java, C++

Each item of slots is a time slot [start, end], and no two slots have the same start. The slot that comes next after a slot is the one with the earliest start among the slots that start at or after its end.

Return a list with one number per slot: the position in slots of the slot that comes next after it, or -1 if there is none. A slot whose start equals its own end comes next after itself.

Example 1

slots [[3,5],[1,2],[5,9],[2,3]]
returns [2,3,-1,0]

After [3, 5] comes the slot that starts at 5, at position 2. After [1, 2] comes [2, 3], and after [2, 3] comes [3, 5]. Nothing starts at 9 or later.

Example 2

slots [[4,4]]
returns [0]

A slot with no length starts at its own end.

Constraints

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Sorting and searching problems