389. Manage Non-Overlapping Car Wash Bay Reservations
Manage Non-Overlapping Car Wash Bay Reservations

A neighborhood car wash has one washing bay. Customers request the bay for different time periods.

Implement the WashBaySchedule class to manage these reservations.

Reservation Rules

Each reservation uses the half-open time range [washStart, washEnd). The bay is occupied from washStart up to, but not including, washEnd.

  • Accept a request only when it does not overlap any previously accepted reservation.
  • When a request is accepted, save it and return true.
  • When a request overlaps an accepted reservation, return false without saving it.
  • A request may start at the exact time another reservation ends.

Class

WashBaySchedule()

Creates an empty schedule for the washing bay.

Method Signature

boolean reserve(int washStart, int washEnd)

Parameters

  • washStart: The starting time of the requested reservation.
  • washEnd: The ending time of the requested reservation.

Return Value

  • Return true if the reservation is accepted and saved.
  • Return false if it overlaps an existing reservation.

Constraints

  • 0 ≤ washStart < washEnd ≤ 1,000,000,000
  • At most 1,000 calls will be made to reserve.

Examples

Example 1

Constructor: WashBaySchedule bay = new WashBaySchedule()

Method Call: bay.reserve(washStart = 75, washEnd = 95)

Output: true

Method Call: bay.reserve(washStart = 40, washEnd = 75)

Output: true

Method Call: bay.reserve(washStart = 90, washEnd = 110)

Output: false

Method Call: bay.reserve(washStart = 95, washEnd = 130)

Output: true

Explanation

The first request reserves the bay from time 75 until time 95. The second request ends exactly at time 75, so it is accepted.

The third request overlaps the first reservation between times 90 and 95, so it is rejected. The final request starts exactly when the first reservation ends, so it is accepted.



Please use Laptop/Desktop or any other large screen to add/edit code.