Busiest Hour — Difference Array & Sweep

Given bike rental start and return hours, report the hour when most bikes were out, earliest hour winning ties.

Difference Array & Sweep · Easy

The problem

Each rental is [start, end]: a bike goes out at hour start and comes back at hour end, so it is out during hours start up to but not including end. Find the hour from 0 to 10 when the most bikes are out. If several hours tie, return the earliest.

The story

A bike stand logs 1 to 8 rentals in one day. Each log gives the hour a bike was taken and the later hour it was returned, both between 0 and 11. A bike counts as out from the hour it is taken until the hour it is returned, and is already back during the return hour. Report the hour when the most bikes were out, taking the earliest hour if several tie.

What to return

Constraints

Example

Input
logs = [[1, 4], [2, 6], [3, 5]]
Output
3

Solve it

More in Difference Array & Sweep

All algorithm problems