Maximum Available Time Slot
In calendaring and scheduling applications, efficiently identifying free time is crucial for user experience and resource management. This problem simulates a core scheduling task: given a user's scheduled events for a day, determine the single longest continuous block of time when they are available.
Your task is to implement a function findMaxAvailableTime that takes a list of scheduled events and returns the longest contiguous time slot where no events are scheduled. The day is considered to be from 0 minutes (midnight) to 1440 minutes (next midnight, exclusive, representing 24 hours).
Specifically, you must:
events, handling any overlaps or adjacent intervals by effectively merging them into continuous blocks of booked time.[0, 1440).[start, end] pair of the single longest free time slot. If multiple slots have the same maximum duration, return the one that starts earliest.null.Function Signature:
function findMaxAvailableTime(events: number[][]): number[] | null
Input:
events: An array of number[] where each inner array [start, end] represents a scheduled event. start is the beginning minute (inclusive) and end is the ending minute (exclusive) of an event. Both start and end are in minutes from the start of the day (0 to 1440).Output:
number[] array [start, end] representing the longest available time slot. If no free time exists, return null.[0, 1440) minutes. Any event intervals provided should be clipped to this range. For example, [-10, 50] becomes [0, 50], and [1400, 1500] becomes [1400, 1440]. If an event becomes invalid after clipping (e.g., [1450, 1460] becomes [1440, 1440], or [500, 400] is provided), it should be ignored.events list: If the input events array is empty, the entire day [0, 1440) is considered free, and this should be the returned value.[start, end) (inclusive start, exclusive end). This means an event [60, 120) ends exactly when [120, 180) begins, with no overlap or gap between them.Examples
Example 1
Input:
events = [[720, 780], [60, 120]]
Output:
[780, 1440]
Note:
The day is [0, 1440). Events are [60, 120) and [720, 780). Free slots are: [0, 60) (duration 60), [120, 720) (duration 600), [780, 1440) (duration 660). The longest free slot is [780, 1440) with a duration of 660 minutes.
Example 2
Input:
events = [[0, 720], [700, 1440]]
Output:
null
Note:
After merging, the events cover the entire day [0, 1440). There are no available time slots.
Example 3
Input:
events = []
Output:
[0, 1440]
Note:
With no scheduled events, the entire day from 0 to 1440 is available.
Example 4
Input:
events = [[0, 60], [1380, 1440], [300, 400], [450, 500]]
Output:
[60, 300]
Note:
The day is [0, 1440). Sorted and merged events: [[0, 60), [300, 400), [450, 500), [1380, 1440)). Free slots:
[60, 300] which is duration 240. This is incorrect. The expected output should be [500, 1380]. Correcting the example and explanation.Corrected Explanation: "The day is [0, 1440). Sorted and merged events: [[0, 60), [300, 400), [450, 500), [1380, 1440)). Potential free slots and their durations:
Constraints
0 <= events.length <= 10^5[start, end] will have 0 <= start < end <= 1440 before any clipping. After clipping, start and end will be within [0, 1440]. An event [100, 100] or [200, 100] is invalid and should be ignored.Loading editor...