Skip to content

feat: add solutions to lc problem: No.1229 #3500

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
86 changes: 48 additions & 38 deletions solution/1200-1299/1229.Meeting Scheduler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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$ 分别为两个数组的长度。

Expand Down Expand Up @@ -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<Vec<i32>>,
slots2: Vec<Vec<i32>>,
duration: i32,
) -> Vec<i32> {
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<Vec<i32>>, mut slots2: Vec<Vec<i32>>, duration: i32) -> Vec<i32> {
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![]
}
}
Expand Down
88 changes: 49 additions & 39 deletions solution/1200-1299/1229.Meeting Scheduler/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.

<!-- tabs:start -->

Expand Down Expand Up @@ -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<Vec<i32>>,
slots2: Vec<Vec<i32>>,
duration: i32,
) -> Vec<i32> {
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<Vec<i32>>, mut slots2: Vec<Vec<i32>>, duration: i32) -> Vec<i32> {
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![]
}
}
Expand Down
50 changes: 19 additions & 31 deletions solution/1200-1299/1229.Meeting Scheduler/Solution.rs
Original file line number Diff line number Diff line change
@@ -1,45 +1,33 @@
impl Solution {
#[allow(dead_code)]
pub fn min_available_duration(
slots1: Vec<Vec<i32>>,
slots2: Vec<Vec<i32>>,
mut slots1: Vec<Vec<i32>>,
mut slots2: Vec<Vec<i32>>,
duration: i32,
) -> Vec<i32> {
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![]
}
}
21 changes: 21 additions & 0 deletions solution/1200-1299/1229.Meeting Scheduler/Solution.ts
Original file line number Diff line number Diff line change
@@ -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 [];
}
Loading