Schedule Tasks with an Interval Tree
Learn this problemProblem statement
Process the ordered scheduling operations in operations. The calendar starts empty, and every time interval is half-open: [start, end).
BOOK id start end: If the interval overlaps any accepted booking, leave the calendar unchanged and appendfalse. Otherwise accept it and appendtrue.CONFLICTS start end: Append the comma-separated IDs of all accepted bookings that overlap the query, ordered by booking start. Append an empty string when none overlap.
Two half-open intervals overlap exactly when leftStart < rightEnd and rightStart < leftEnd. Each BOOK ID is unique across the input.
Function
scheduleTasks(operations: String[]) → String[]Examples
Example 1
operations = ["BOOK focus 9 10","BOOK standup 10 11","BOOK overlap 9 12","CONFLICTS 9 12","BOOK lunch 12 13","CONFLICTS 11 13"]return = ["true","true","false","focus,standup","true","lunch"]focus and standup only touch, so both are accepted. overlap intersects them and is rejected. The first query returns accepted IDs in start order; the second reaches only lunch.
Example 2
operations = ["BOOK night 20 30","BOOK touch_left 10 20","BOOK touch_right 30 40","CONFLICTS 19 31","BOOK duplicate_start 20 21"]return = ["true","true","true","touch_left,night,touch_right","false"]The first three bookings are disjoint under half-open boundaries even though they arrive out of start order. The wide query intersects all three. The final booking overlaps night and is rejected.
Example 3
operations = ["CONFLICTS 0 100","BOOK only 40 60","CONFLICTS 0 40","CONFLICTS 40 41"]return = ["","true","","only"]The empty calendar and a query ending exactly where only starts produce empty strings. A query beginning at that boundary overlaps the booking.
Constraints
1 <= operations.length <= 4000.- Every operation has one of the documented forms.
0 <= start < end <= 10^9.- Booking IDs contain from
1through20lowercase ASCII letters, digits, or underscores. - Every
BOOKID is unique across all attempts.