diff --git a/solution/1200-1299/1229.Meeting Scheduler/README.md b/solution/1200-1299/1229.Meeting Scheduler/README.md index f09c733f597e3..c1d8ccdbacabb 100644 --- a/solution/1200-1299/1229.Meeting Scheduler/README.md +++ b/solution/1200-1299/1229.Meeting Scheduler/README.md @@ -65,7 +65,7 @@ tags: ### 方法一:排序 + 双指针 -我们可以将两个人的空闲时间分别排序,然后使用双指针遍历两个数组,找到两个人的空闲时间段的交集,如果交集的长度大于等于 `duration`,则返回交集的起始时间和起始时间加上 `duration`。 +我们可以将两个人的空闲时间分别排序,然后使用双指针遍历两个数组,找到两个人的空闲时间段的交集,如果交集的长度大于等于 `duration`,则返回交集的起始时间和起始时间加上 `duration`。否则,如果第一个人的空闲时间段的结束时间小于第二个人的空闲时间段的结束时间,我们就移动第一个人的指针,否则移动第二个人的指针。继续遍历,直到找到满足条件的时间段或者遍历结束。 时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(\log m + \log n)$。其中 $m$ 和 $n$ 分别为两个数组的长度。 @@ -170,51 +170,61 @@ func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int { } ``` +#### TypeScript + +```ts +function minAvailableDuration(slots1: number[][], slots2: number[][], duration: number): number[] { + slots1.sort((a, b) => a[0] - b[0]); + slots2.sort((a, b) => a[0] - b[0]); + const [m, n] = [slots1.length, slots2.length]; + let [i, j] = [0, 0]; + while (i < m && j < n) { + const [start1, end1] = slots1[i]; + const [start2, end2] = slots2[j]; + const start = Math.max(start1, start2); + const end = Math.min(end1, end2); + if (end - start >= duration) { + return [start, start + duration]; + } + if (end1 < end2) { + i++; + } else { + j++; + } + } + return []; +} +``` + #### Rust ```rust impl Solution { - #[allow(dead_code)] - pub fn min_available_duration( - slots1: Vec>, - slots2: Vec>, - duration: i32, - ) -> Vec { - let mut slots1 = slots1; - let mut slots2 = slots2; - - // First sort the two vectors based on the beginning time - slots1.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0])); - slots2.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0])); - - // Then traverse the two vector - let mut i: usize = 0; - let mut j: usize = 0; - let N = slots1.len(); - let M = slots2.len(); - - while i < N && j < M { - let (start, end) = (slots1[i][0], slots1[i][1]); - while j < M && slots2[j][0] < end { - // If still in the scope - let (cur_x, cur_y) = ( - std::cmp::max(start, slots2[j][0]), - std::cmp::min(end, slots2[j][1]), - ); - if cur_y - cur_x >= duration { - return vec![cur_x, cur_x + duration]; - } - // Otherwise, keep iterating - if slots1[i][1] < slots2[j][1] { - // Update i then - break; - } + pub fn min_available_duration(mut slots1: Vec>, mut slots2: Vec>, duration: i32) -> Vec { + slots1.sort_by_key(|slot| slot[0]); + slots2.sort_by_key(|slot| slot[0]); + + let (mut i, mut j) = (0, 0); + let (m, n) = (slots1.len(), slots2.len()); + + while i < m && j < n { + let (start1, end1) = (slots1[i][0], slots1[i][1]); + let (start2, end2) = (slots2[j][0], slots2[j][1]); + + let start = start1.max(start2); + let end = end1.min(end2); + + if end - start >= duration { + return vec![start, start + duration]; + } + + if end1 < end2 { + i += 1; + } else { j += 1; } - i += 1; } - // The default is an empty vector vec![] } } diff --git a/solution/1200-1299/1229.Meeting Scheduler/README_EN.md b/solution/1200-1299/1229.Meeting Scheduler/README_EN.md index 2bb8f66336b95..d1574290be50d 100644 --- a/solution/1200-1299/1229.Meeting Scheduler/README_EN.md +++ b/solution/1200-1299/1229.Meeting Scheduler/README_EN.md @@ -63,9 +63,9 @@ tags: ### Solution 1: Sorting + Two Pointers -We can sort the free time of the two people separately, then use two pointers to traverse the two arrays, find the intersection of the free time periods of the two people, and if the length of the intersection is greater than or equal to `duration`, then return the start time of the intersection and the start time plus `duration`. +We can sort the free time intervals of both people, then use two pointers to traverse the two arrays and find the intersection of the free time intervals of both people. If the length of the intersection is greater than or equal to `duration`, return the start time of the intersection and the start time plus `duration`. Otherwise, if the end time of the first person's free time interval is less than the end time of the second person's free time interval, move the first person's pointer; otherwise, move the second person's pointer. Continue traversing until a suitable time interval is found or the traversal ends. -The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(\log m + \log n)$. Where $m$ and $n$ are the lengths of the two arrays respectively. +The time complexity is $O(m \times \log m + n \times \log n)$, and the space complexity is $O(\log m + \log n)$. Here, $m$ and $n$ are the lengths of the two arrays, respectively. @@ -168,51 +168,61 @@ func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int { } ``` +#### TypeScript + +```ts +function minAvailableDuration(slots1: number[][], slots2: number[][], duration: number): number[] { + slots1.sort((a, b) => a[0] - b[0]); + slots2.sort((a, b) => a[0] - b[0]); + const [m, n] = [slots1.length, slots2.length]; + let [i, j] = [0, 0]; + while (i < m && j < n) { + const [start1, end1] = slots1[i]; + const [start2, end2] = slots2[j]; + const start = Math.max(start1, start2); + const end = Math.min(end1, end2); + if (end - start >= duration) { + return [start, start + duration]; + } + if (end1 < end2) { + i++; + } else { + j++; + } + } + return []; +} +``` + #### Rust ```rust impl Solution { - #[allow(dead_code)] - pub fn min_available_duration( - slots1: Vec>, - slots2: Vec>, - duration: i32, - ) -> Vec { - let mut slots1 = slots1; - let mut slots2 = slots2; - - // First sort the two vectors based on the beginning time - slots1.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0])); - slots2.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0])); - - // Then traverse the two vector - let mut i: usize = 0; - let mut j: usize = 0; - let N = slots1.len(); - let M = slots2.len(); - - while i < N && j < M { - let (start, end) = (slots1[i][0], slots1[i][1]); - while j < M && slots2[j][0] < end { - // If still in the scope - let (cur_x, cur_y) = ( - std::cmp::max(start, slots2[j][0]), - std::cmp::min(end, slots2[j][1]), - ); - if cur_y - cur_x >= duration { - return vec![cur_x, cur_x + duration]; - } - // Otherwise, keep iterating - if slots1[i][1] < slots2[j][1] { - // Update i then - break; - } + pub fn min_available_duration(mut slots1: Vec>, mut slots2: Vec>, duration: i32) -> Vec { + slots1.sort_by_key(|slot| slot[0]); + slots2.sort_by_key(|slot| slot[0]); + + let (mut i, mut j) = (0, 0); + let (m, n) = (slots1.len(), slots2.len()); + + while i < m && j < n { + let (start1, end1) = (slots1[i][0], slots1[i][1]); + let (start2, end2) = (slots2[j][0], slots2[j][1]); + + let start = start1.max(start2); + let end = end1.min(end2); + + if end - start >= duration { + return vec![start, start + duration]; + } + + if end1 < end2 { + i += 1; + } else { j += 1; } - i += 1; } - // The default is an empty vector vec![] } } diff --git a/solution/1200-1299/1229.Meeting Scheduler/Solution.rs b/solution/1200-1299/1229.Meeting Scheduler/Solution.rs index 6d9f5c62d4718..43e1022d2623d 100644 --- a/solution/1200-1299/1229.Meeting Scheduler/Solution.rs +++ b/solution/1200-1299/1229.Meeting Scheduler/Solution.rs @@ -1,45 +1,33 @@ impl Solution { - #[allow(dead_code)] pub fn min_available_duration( - slots1: Vec>, - slots2: Vec>, + mut slots1: Vec>, + mut slots2: Vec>, duration: i32, ) -> Vec { - let mut slots1 = slots1; - let mut slots2 = slots2; + slots1.sort_by_key(|slot| slot[0]); + slots2.sort_by_key(|slot| slot[0]); - // First sort the two vectors based on the beginning time - slots1.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0])); - slots2.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0])); + let (mut i, mut j) = (0, 0); + let (m, n) = (slots1.len(), slots2.len()); - // Then traverse the two vector - let mut i: usize = 0; - let mut j: usize = 0; - let N = slots1.len(); - let M = slots2.len(); + while i < m && j < n { + let (start1, end1) = (slots1[i][0], slots1[i][1]); + let (start2, end2) = (slots2[j][0], slots2[j][1]); - while i < N && j < M { - let (start, end) = (slots1[i][0], slots1[i][1]); - while j < M && slots2[j][0] < end { - // If still in the scope - let (cur_x, cur_y) = ( - std::cmp::max(start, slots2[j][0]), - std::cmp::min(end, slots2[j][1]), - ); - if cur_y - cur_x >= duration { - return vec![cur_x, cur_x + duration]; - } - // Otherwise, keep iterating - if slots1[i][1] < slots2[j][1] { - // Update i then - break; - } + let start = start1.max(start2); + let end = end1.min(end2); + + if end - start >= duration { + return vec![start, start + duration]; + } + + if end1 < end2 { + i += 1; + } else { j += 1; } - i += 1; } - // The default is an empty vector vec![] } } diff --git a/solution/1200-1299/1229.Meeting Scheduler/Solution.ts b/solution/1200-1299/1229.Meeting Scheduler/Solution.ts new file mode 100644 index 0000000000000..fa2a05985ec96 --- /dev/null +++ b/solution/1200-1299/1229.Meeting Scheduler/Solution.ts @@ -0,0 +1,21 @@ +function minAvailableDuration(slots1: number[][], slots2: number[][], duration: number): number[] { + slots1.sort((a, b) => a[0] - b[0]); + slots2.sort((a, b) => a[0] - b[0]); + const [m, n] = [slots1.length, slots2.length]; + let [i, j] = [0, 0]; + while (i < m && j < n) { + const [start1, end1] = slots1[i]; + const [start2, end2] = slots2[j]; + const start = Math.max(start1, start2); + const end = Math.min(end1, end2); + if (end - start >= duration) { + return [start, start + duration]; + } + if (end1 < end2) { + i++; + } else { + j++; + } + } + return []; +}