Problem Description
Given two arrays slots1 and slots2 where each element represents an available time slot in the form [start, end] for two people, determine the earliest time slot which is common to both and whose duration is at least the specified duration. If no such time slot exists, return an empty array.
Key Insights
- Sort both time slot arrays by their start times to simplify the overlap checking process.
- Use a two-pointer approach to traverse the sorted arrays concurrently.
- For each pair of slots examined, compute the intersection by taking the maximum of the start times and the minimum of the end times.
- Check if this overlapping duration is at least as long as the required meeting duration; if so, return the earliest valid meeting interval.
- Advance the pointer corresponding to the slot that ends earlier to look for a potentially better overlapping slot further on.
Space and Time Complexity
Time Complexity: O(n log n + m log m), where n and m are the number of slots in slots1 and slots2, respectively, due to sorting. The merging process itself runs in O(n + m). Space Complexity: O(1) additional space (ignoring the space used for sorting if done in-place).
Solution
The solution involves first sorting both slots1 and slots2 by their start times. Then, using a two-pointer approach, iterate over both sorted lists simultaneously. For each pair of slots, calculate the possible overlapping time interval by taking max(slot1_start, slot2_start) as the start and min(slot1_end, slot2_end) as the end. If the duration of the overlap is at least as long as the required duration, the earliest feasible meeting time is found and we return the interval starting at this overlap start and ending at (overlap start + duration). If the overlap is insufficient, advance the pointer of the slot that ends first since that slot cannot provide a sufficient overlap with any future slots from the other list.