This problem requires finding the earliest time slot of a specified duration (duration
) that is available for two people, given their respective availability slots (slots1
and slots2
).
The optimal approach combines sorting and a two-pointer technique for efficient searching. Here's a breakdown:
Sorting: We begin by sorting both slots1
and slots2
based on the start time of each interval. This allows us to iterate through the slots in increasing order of availability. This step takes O(m log m + n log n) time, where 'm' and 'n' are the lengths of slots1
and slots2
respectively.
Two Pointers: We use two pointers, i
and j
, to iterate through the sorted slots1
and slots2
arrays, respectively. At each step:
start
time as the maximum of the current start times from both arrays (both people must be available).end
time as the minimum of the current end times from both arrays (the meeting must end within both people's availability).end - start
is greater than or equal to duration
. If it is, we've found a suitable time slot and return [start, start + duration]
.No Common Slot: If the pointers reach the end of either array without finding a suitable slot, it means there's no common time slot that meets the duration requirement, so we return an empty array.
The code examples below demonstrate the solution in various programming languages. The core logic remains the same across all implementations.
def minAvailableDuration(slots1: List[List[int]], slots2: List[List[int]], duration: int) -> List[int]:
slots1.sort() # Sort slots1 by start time
slots2.sort() # Sort slots2 by start time
i = j = 0
while i < len(slots1) and j < len(slots2): # Iterate until end of either array
start = max(slots1[i][0], slots2[j][0]) # Find common start time
end = min(slots1[i][1], slots2[j][1]) # Find common end time
if end - start >= duration: # Check if duration is met
return [start, start + duration] # Return the found slot
if slots1[i][1] < slots2[j][1]: # Advance the pointer to the interval ending sooner
i += 1
else:
j += 1
return [] # No suitable slot found
import java.util.*;
class Solution {
public List<Integer> minAvailableDuration(int[][] slots1, int[][] slots2, int duration) {
Arrays.sort(slots1, (a, b) -> a[0] - b[0]); // Sort slots1
Arrays.sort(slots2, (a, b) -> a[0] - b[0]); // Sort slots2
int i = 0, j = 0;
while (i < slots1.length && j < slots2.length) { // Iterate until end of either array
int start = Math.max(slots1[i][0], slots2[j][0]); // Find common start
int end = Math.min(slots1[i][1], slots2[j][1]); // Find common end
if (end - start >= duration) { // Check for sufficient duration
return Arrays.asList(start, start + duration); // Return found slot
}
if (slots1[i][1] < slots2[j][1]) { // Advance pointer to sooner-ending interval
i++;
} else {
j++;
}
}
return new ArrayList<>(); // No suitable slot found
}
}
The other languages (C++, Go, TypeScript, Rust) would follow a very similar structure, with the main differences being in the syntax for sorting, array handling, and function definitions. The core algorithm remains the same.