Bookings at One Minute
Easy · Binary Search · about 15 min
A booking system keeps the start minute of every booking in a list sorted from earliest to latest. Many bookings can share the same minute, so values can repeat.
Given the sorted list minutes and one minute t, return [first, last]: the index of the first and the last booking at minute t. If there is no booking at t, return [-1, -1]. Use binary search: the list can be huge.
Example: booking_minute_range([1, 3, 3, 3, 5, 8], 3) → [1, 3]
Example: booking_minute_range([1, 3, 3, 3, 5, 8], 4) → [-1, -1]
Constraints: 0 to 200,000,000 bookings; minutes are integers (they can be negative for bookings made before the day starts).
Hints
- Can one binary search find the first index whose value is at least some number?
- The first booking at t is the first index with value at least t. The last is one before the first index with value at least t + 1.
- Before answering, check that the first index is inside the list and really holds t.
Learn the pattern first: the Binary Search lesson.
More Binary Search problems
- Biggest Square of Tiles (Easy)
- Find a Book on a Rotated Shelf (Medium)
- Earliest Entry in a Ring Log (Medium)
- Slowest Reading Pace (Medium)
- Fair Editing Shifts (Hard)
- K-th Cheapest Across Two Stores (Hard)