You are given a list of scheduled events for a single day. Each event is represented by a pair of numbers [start, end]
, where start
and end
denote the start and end times (in 24-hour format, e.g., 9 for 9:00 AM, 17 for 5:00 PM). The existing events do not overlap with each other.
Your task is to write a function that, given the current schedule and a new event (also represented as [start, end]
), determines if the new event can be added to the schedule without causing any overlaps.
Write a function that takes the following parameters:
existingEvents
: an array of events (each event is an array [start, end]
) sorted by the start times.newEvent
: an array [start, end]
representing the new event to be scheduled.The function should return true
if the newEvent
can be added without overlapping any existing event, and false
otherwise.
Assume all events are within the same day and times are represented as integers (e.g., 0 to 24).
Consider edge cases such as when the new event starts exactly when an existing event ends or ends exactly when an existing event starts.
// Example 1:
const existingEvents = [[9, 11], [13, 15], [16, 18]];
const newEvent = [11, 13];
// Expected output: true (newEvent fits between the existing events without overlaps)
// Example 2:
const existingEvents = [[9, 11], [13, 15], [16, 18]];
const newEvent = [10, 12];
// Expected output: false (newEvent overlaps with the event [9, 11])
Implement the function following the requirements stated. Make sure your solution handles all edge cases and is efficient.