Meeting Rooms by Number
Implement a class RoomBooker for k meeting rooms numbered 1 to k. A booking holds a room from start up to, but not including, end, so a meeting that ends at 5 does not clash with one that starts at 5. Write these methods:
RoomBooker(k)starts with every room free.book(start, end)puts the meeting in the lowest-numbered room that has no booking overlapping it. Returns the room number, or-1if every room has a clash (then nothing changes).cancel(room, start)removes the booking ofroomthat starts atstartand returnstrue, or returnsfalseif there is none.rooms_in_use(t)returns how many rooms have a booking that covers timet.bookings_of(room)returns the bookings ofroomas[start, end]pairs, sorted by start.
room is always between 1 and k. The tests call the methods in sequence and compare every return value. The expected list holds null for the constructor.
Example 1
RoomBooker(2)
book(1, 5) returns 1
book(3, 7) returns 2
book(5, 6) returns 1
book(2, 4) returns -1
rooms_in_use(5) returns 2
cancel(1, 1) returns true
book(2, 4) returns 1
bookings_of(1) returns [[2,4],[5,6]]
The meeting from 5 to 6 fits in room 1 because the first meeting there ends exactly at 5. The meeting from 2 to 4 clashes in both rooms until the first booking of room 1 is cancelled.
Constraints
1 <= k <= 40 <= start < end <= 500 <= t <= 50- Each test makes
1to40method calls after the constructor.
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.