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] < slots1[i]`
• `slots2[i] < slots2[i]`
• `0 <= slots1[i][j], slots2[i][j] <= 109`
• `1 <= duration <= 106`

1229. Meeting Scheduler
``````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);
slots2.sort_unstable_by_key(|v| v);
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 < s2 {
i += 1;
continue;
}
if s2 < s1 {
j += 1;
continue;
}
let start = s1.max(s2);
let end = s1.min(s2);
if end - start >= duration {
return vec![start, start + duration];
} else {
if s1 < s2 {
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
);
}
``````