1229. Meeting Scheduler
Given the availability time slots arrays slots1
and slots2
of two people and a meeting duration duration
, return the earliest time slot that works for both of them and is of duration duration
.
If there is no common time slot that satisfies the requirements, return an empty array.
The format of a time slot is an array of two elements [start, end]
representing an inclusive time range from start
to end
.
It is guaranteed that no two availability slots of the same person intersect with each other. That is, for any two time slots [start1, end1]
and [start2, end2]
of the same person, either start1 > end2
or start2 > end1
.
Example 1:
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 8 Output: [60,68]
Example 2:
Input: slots1 = [[10,50],[60,120],[140,210]], slots2 = [[0,15],[60,70]], duration = 12 Output: []
Constraints:
1 <= slots1.length, slots2.length <= 104
slots1[i].length, slots2[i].length == 2
slots1[i][0] < slots1[i][1]
slots2[i][0] < slots2[i][1]
0 <= slots1[i][j], slots2[i][j] <= 109
1 <= duration <= 106
Rust Solution
struct Solution;
impl Solution {
fn min_available_duration(
mut slots1: Vec<Vec<i32>>,
mut slots2: Vec<Vec<i32>>,
duration: i32,
) -> Vec<i32> {
slots1.sort_unstable_by_key(|v| v[0]);
slots2.sort_unstable_by_key(|v| v[0]);
let n = slots1.len();
let m = slots2.len();
let mut i = 0;
let mut j = 0;
while i < n && j < m {
let s1 = &slots1[i];
let s2 = &slots2[j];
if s1[1] < s2[0] {
i += 1;
continue;
}
if s2[1] < s1[0] {
j += 1;
continue;
}
let start = s1[0].max(s2[0]);
let end = s1[1].min(s2[1]);
if end - start >= duration {
return vec![start, start + duration];
} else {
if s1[0] < s2[0] {
i += 1;
} else {
j += 1;
}
}
}
vec![]
}
}
#[test]
fn test() {
let slots1 = vec_vec_i32![[10, 50], [60, 120], [140, 210]];
let slots2 = vec_vec_i32![[0, 15], [60, 70]];
let duration = 8;
let res = vec![60, 68];
assert_eq!(
Solution::min_available_duration(slots1, slots2, duration),
res
);
let slots1 = vec_vec_i32![[10, 50], [60, 120], [140, 210]];
let slots2 = vec_vec_i32![[0, 15], [60, 70]];
let duration = 12;
let res: Vec<i32> = vec![];
assert_eq!(
Solution::min_available_duration(slots1, slots2, duration),
res
);
}
Having problems with this solution? Click here to submit an issue on github.