463. Assign Tennis Court Bookings With Maintenance
Assign Tennis Court Bookings With Maintenance

A tennis club has an unlimited number of courts. You are given a list of bookings, each with an id, a start time and a finish time. Assign every booking to a court so that no court is used by two bookings at the same time.

A booking uses its court from startTime up to, but not including, finishTime, so a booking can start on a court at the exact moment the previous booking there finishes.

Courts may also need maintenance. A court is closed for maintenanceTime time units after every durability bookings it hosts. If the booking that triggers maintenance finishes at time t, the court is free again at time t + maintenanceTime.

Assignment Rule

Courts are numbered 0, 1, 2, ... in the order they are first used. To keep the answer unique, assign bookings one at a time:

  • Process bookings in increasing order of startTime. If two bookings start at the same time, process the one with the smaller bookingId first.
  • Assign the booking to the lowest-numbered court that is free at its startTime. A court is free if it is not hosting a booking and not under maintenance.
  • If no court is free, open a new court with the next number.

Without maintenance, this rule always uses the minimum possible number of courts.

Methods

List<String> assignCourts(List<String> bookings)
  • Each element of bookings is "bookingId,startTime,finishTime".
  • Courts never need maintenance.
  • Returns one string per court: the element at index i lists the ids of the bookings on court i in increasing order of startTime, separated by commas.
List<String> assignCourtsWithMaintenance(List<String> bookings, int maintenanceTime, int durability)
  • Same as assignCourts, but every court needs maintenance as described above.
  • When durability = 1, a court needs maintenance after every booking.
  • Returns the courts in the same format as assignCourts.

Constraints

  • 1 ≤ bookings.size() ≤ 100,000
  • 1 ≤ bookingId ≤ 1,000,000,000
  • All bookingId values are distinct.
  • 0 ≤ startTime < finishTime ≤ 1,000,000,000
  • 0 ≤ maintenanceTime ≤ 1,000,000,000
  • 1 ≤ durability ≤ 100,000

Examples

Example 1

Input: assignCourts(bookings = ["11,1,4", "12,2,5", "13,4,6", "14,5,8", "15,7,9"])

Output: ["11,13,15", "12,14"]

Explanation:

  • Booking 11 opens court 0. Booking 12 starts at 2 while court 0 is busy, so it opens court 1.
  • Booking 13 starts at 4, exactly when booking 11 finishes, so it takes court 0.
  • Booking 14 starts at 5. Court 0 is busy until 6 and court 1 is free, so it takes court 1.
  • Booking 15 starts at 7, when court 0 is free again.
  • Bookings 11 and 12 overlap, so at least 2 courts are needed.

Example 2

Input: assignCourts(bookings = ["7,10,20", "5,12,18", "3,10,15", "2,18,21", "9,15,22", "4,23,26"])

Output: ["3,9,4", "7", "5,2"]

Explanation:

  • Bookings 3 and 7 both start at 10. Booking 3 has the smaller id, so it goes first and opens court 0. Booking 7 opens court 1.
  • Booking 5 starts at 12 while courts 0 and 1 are busy, so it opens court 2.
  • Booking 9 starts at 15, when court 0 becomes free. Booking 2 starts at 18, when court 2 becomes free.
  • Booking 4 starts at 23, when all three courts are free, so it takes the lowest-numbered one, court 0.

Example 3

Input: assignCourtsWithMaintenance(bookings = ["11,1,4", "12,2,5", "13,4,6", "14,5,8", "15,7,9"], maintenanceTime = 1, durability = 1)

Output: ["11,14", "12,15", "13"]

Explanation:

  • These are the bookings of Example 1, but each court now needs 1 time unit of maintenance after every booking.
  • At time 4, court 0 is under maintenance until 5 and court 1 is still hosting booking 12, so booking 13 opens court 2.
  • Booking 14 starts at 5, when court 0 is free again. Booking 15 starts at 7 and takes court 1, which has been free since 6.

Example 4

Input: assignCourtsWithMaintenance(bookings = ["21,0,10", "22,10,20", "23,20,30", "24,30,40", "25,35,45"], maintenanceTime = 15, durability = 2)

Output: ["21,22,25", "23,24"]

Explanation:

  • Court 0 hosts bookings 21 and 22, then is under maintenance until 35.
  • Booking 23 starts at 20, during that maintenance, so it opens court 1. Booking 24 is the second booking on court 1, so court 1 is under maintenance from 40 until 55.
  • Booking 25 starts at 35, exactly when the maintenance of court 0 ends, so it takes court 0.


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