Skip to content

Commit 919c842

Browse files
authored
feat: add solutions to lc problem: No.1229 (doocs#3500)
No.1229.Meeting Scheduler
1 parent fb2372e commit 919c842

File tree

4 files changed

+137
-108
lines changed

4 files changed

+137
-108
lines changed

solution/1200-1299/1229.Meeting Scheduler/README.md

Lines changed: 48 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ tags:
6565

6666
### 方法一:排序 + 双指针
6767

68-
我们可以将两个人的空闲时间分别排序,然后使用双指针遍历两个数组,找到两个人的空闲时间段的交集,如果交集的长度大于等于 `duration`,则返回交集的起始时间和起始时间加上 `duration`
68+
我们可以将两个人的空闲时间分别排序,然后使用双指针遍历两个数组,找到两个人的空闲时间段的交集,如果交集的长度大于等于 `duration`,则返回交集的起始时间和起始时间加上 `duration`否则,如果第一个人的空闲时间段的结束时间小于第二个人的空闲时间段的结束时间,我们就移动第一个人的指针,否则移动第二个人的指针。继续遍历,直到找到满足条件的时间段或者遍历结束。
6969

7070
时间复杂度 $O(m \times \log m + n \times \log n)$,空间复杂度 $O(\log m + \log n)$。其中 $m$ 和 $n$ 分别为两个数组的长度。
7171

@@ -170,51 +170,61 @@ func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int {
170170
}
171171
```
172172

173+
#### TypeScript
174+
175+
```ts
176+
function minAvailableDuration(slots1: number[][], slots2: number[][], duration: number): number[] {
177+
slots1.sort((a, b) => a[0] - b[0]);
178+
slots2.sort((a, b) => a[0] - b[0]);
179+
const [m, n] = [slots1.length, slots2.length];
180+
let [i, j] = [0, 0];
181+
while (i < m && j < n) {
182+
const [start1, end1] = slots1[i];
183+
const [start2, end2] = slots2[j];
184+
const start = Math.max(start1, start2);
185+
const end = Math.min(end1, end2);
186+
if (end - start >= duration) {
187+
return [start, start + duration];
188+
}
189+
if (end1 < end2) {
190+
i++;
191+
} else {
192+
j++;
193+
}
194+
}
195+
return [];
196+
}
197+
```
198+
173199
#### Rust
174200

175201
```rust
176202
impl Solution {
177-
#[allow(dead_code)]
178-
pub fn min_available_duration(
179-
slots1: Vec<Vec<i32>>,
180-
slots2: Vec<Vec<i32>>,
181-
duration: i32,
182-
) -> Vec<i32> {
183-
let mut slots1 = slots1;
184-
let mut slots2 = slots2;
185-
186-
// First sort the two vectors based on the beginning time
187-
slots1.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
188-
slots2.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
189-
190-
// Then traverse the two vector
191-
let mut i: usize = 0;
192-
let mut j: usize = 0;
193-
let N = slots1.len();
194-
let M = slots2.len();
195-
196-
while i < N && j < M {
197-
let (start, end) = (slots1[i][0], slots1[i][1]);
198-
while j < M && slots2[j][0] < end {
199-
// If still in the scope
200-
let (cur_x, cur_y) = (
201-
std::cmp::max(start, slots2[j][0]),
202-
std::cmp::min(end, slots2[j][1]),
203-
);
204-
if cur_y - cur_x >= duration {
205-
return vec![cur_x, cur_x + duration];
206-
}
207-
// Otherwise, keep iterating
208-
if slots1[i][1] < slots2[j][1] {
209-
// Update i then
210-
break;
211-
}
203+
pub fn min_available_duration(mut slots1: Vec<Vec<i32>>, mut slots2: Vec<Vec<i32>>, duration: i32) -> Vec<i32> {
204+
slots1.sort_by_key(|slot| slot[0]);
205+
slots2.sort_by_key(|slot| slot[0]);
206+
207+
let (mut i, mut j) = (0, 0);
208+
let (m, n) = (slots1.len(), slots2.len());
209+
210+
while i < m && j < n {
211+
let (start1, end1) = (slots1[i][0], slots1[i][1]);
212+
let (start2, end2) = (slots2[j][0], slots2[j][1]);
213+
214+
let start = start1.max(start2);
215+
let end = end1.min(end2);
216+
217+
if end - start >= duration {
218+
return vec![start, start + duration];
219+
}
220+
221+
if end1 < end2 {
222+
i += 1;
223+
} else {
212224
j += 1;
213225
}
214-
i += 1;
215226
}
216227

217-
// The default is an empty vector
218228
vec![]
219229
}
220230
}

solution/1200-1299/1229.Meeting Scheduler/README_EN.md

Lines changed: 49 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ tags:
6363

6464
### Solution 1: Sorting + Two Pointers
6565

66-
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`.
66+
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.
6767

68-
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.
68+
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.
6969

7070
<!-- tabs:start -->
7171

@@ -168,51 +168,61 @@ func minAvailableDuration(slots1 [][]int, slots2 [][]int, duration int) []int {
168168
}
169169
```
170170

171+
#### TypeScript
172+
173+
```ts
174+
function minAvailableDuration(slots1: number[][], slots2: number[][], duration: number): number[] {
175+
slots1.sort((a, b) => a[0] - b[0]);
176+
slots2.sort((a, b) => a[0] - b[0]);
177+
const [m, n] = [slots1.length, slots2.length];
178+
let [i, j] = [0, 0];
179+
while (i < m && j < n) {
180+
const [start1, end1] = slots1[i];
181+
const [start2, end2] = slots2[j];
182+
const start = Math.max(start1, start2);
183+
const end = Math.min(end1, end2);
184+
if (end - start >= duration) {
185+
return [start, start + duration];
186+
}
187+
if (end1 < end2) {
188+
i++;
189+
} else {
190+
j++;
191+
}
192+
}
193+
return [];
194+
}
195+
```
196+
171197
#### Rust
172198

173199
```rust
174200
impl Solution {
175-
#[allow(dead_code)]
176-
pub fn min_available_duration(
177-
slots1: Vec<Vec<i32>>,
178-
slots2: Vec<Vec<i32>>,
179-
duration: i32,
180-
) -> Vec<i32> {
181-
let mut slots1 = slots1;
182-
let mut slots2 = slots2;
183-
184-
// First sort the two vectors based on the beginning time
185-
slots1.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
186-
slots2.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
187-
188-
// Then traverse the two vector
189-
let mut i: usize = 0;
190-
let mut j: usize = 0;
191-
let N = slots1.len();
192-
let M = slots2.len();
193-
194-
while i < N && j < M {
195-
let (start, end) = (slots1[i][0], slots1[i][1]);
196-
while j < M && slots2[j][0] < end {
197-
// If still in the scope
198-
let (cur_x, cur_y) = (
199-
std::cmp::max(start, slots2[j][0]),
200-
std::cmp::min(end, slots2[j][1]),
201-
);
202-
if cur_y - cur_x >= duration {
203-
return vec![cur_x, cur_x + duration];
204-
}
205-
// Otherwise, keep iterating
206-
if slots1[i][1] < slots2[j][1] {
207-
// Update i then
208-
break;
209-
}
201+
pub fn min_available_duration(mut slots1: Vec<Vec<i32>>, mut slots2: Vec<Vec<i32>>, duration: i32) -> Vec<i32> {
202+
slots1.sort_by_key(|slot| slot[0]);
203+
slots2.sort_by_key(|slot| slot[0]);
204+
205+
let (mut i, mut j) = (0, 0);
206+
let (m, n) = (slots1.len(), slots2.len());
207+
208+
while i < m && j < n {
209+
let (start1, end1) = (slots1[i][0], slots1[i][1]);
210+
let (start2, end2) = (slots2[j][0], slots2[j][1]);
211+
212+
let start = start1.max(start2);
213+
let end = end1.min(end2);
214+
215+
if end - start >= duration {
216+
return vec![start, start + duration];
217+
}
218+
219+
if end1 < end2 {
220+
i += 1;
221+
} else {
210222
j += 1;
211223
}
212-
i += 1;
213224
}
214225

215-
// The default is an empty vector
216226
vec![]
217227
}
218228
}
Lines changed: 19 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,33 @@
11
impl Solution {
2-
#[allow(dead_code)]
32
pub fn min_available_duration(
4-
slots1: Vec<Vec<i32>>,
5-
slots2: Vec<Vec<i32>>,
3+
mut slots1: Vec<Vec<i32>>,
4+
mut slots2: Vec<Vec<i32>>,
65
duration: i32,
76
) -> Vec<i32> {
8-
let mut slots1 = slots1;
9-
let mut slots2 = slots2;
7+
slots1.sort_by_key(|slot| slot[0]);
8+
slots2.sort_by_key(|slot| slot[0]);
109

11-
// First sort the two vectors based on the beginning time
12-
slots1.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
13-
slots2.sort_by(|lhs, rhs| lhs[0].cmp(&rhs[0]));
10+
let (mut i, mut j) = (0, 0);
11+
let (m, n) = (slots1.len(), slots2.len());
1412

15-
// Then traverse the two vector
16-
let mut i: usize = 0;
17-
let mut j: usize = 0;
18-
let N = slots1.len();
19-
let M = slots2.len();
13+
while i < m && j < n {
14+
let (start1, end1) = (slots1[i][0], slots1[i][1]);
15+
let (start2, end2) = (slots2[j][0], slots2[j][1]);
2016

21-
while i < N && j < M {
22-
let (start, end) = (slots1[i][0], slots1[i][1]);
23-
while j < M && slots2[j][0] < end {
24-
// If still in the scope
25-
let (cur_x, cur_y) = (
26-
std::cmp::max(start, slots2[j][0]),
27-
std::cmp::min(end, slots2[j][1]),
28-
);
29-
if cur_y - cur_x >= duration {
30-
return vec![cur_x, cur_x + duration];
31-
}
32-
// Otherwise, keep iterating
33-
if slots1[i][1] < slots2[j][1] {
34-
// Update i then
35-
break;
36-
}
17+
let start = start1.max(start2);
18+
let end = end1.min(end2);
19+
20+
if end - start >= duration {
21+
return vec![start, start + duration];
22+
}
23+
24+
if end1 < end2 {
25+
i += 1;
26+
} else {
3727
j += 1;
3828
}
39-
i += 1;
4029
}
4130

42-
// The default is an empty vector
4331
vec![]
4432
}
4533
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
function minAvailableDuration(slots1: number[][], slots2: number[][], duration: number): number[] {
2+
slots1.sort((a, b) => a[0] - b[0]);
3+
slots2.sort((a, b) => a[0] - b[0]);
4+
const [m, n] = [slots1.length, slots2.length];
5+
let [i, j] = [0, 0];
6+
while (i < m && j < n) {
7+
const [start1, end1] = slots1[i];
8+
const [start2, end2] = slots2[j];
9+
const start = Math.max(start1, start2);
10+
const end = Math.min(end1, end2);
11+
if (end - start >= duration) {
12+
return [start, start + duration];
13+
}
14+
if (end1 < end2) {
15+
i++;
16+
} else {
17+
j++;
18+
}
19+
}
20+
return [];
21+
}

0 commit comments

Comments
 (0)