Skip to content

feat: add rust solution to lc problem: No.1229 #1239

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 2 commits into from
Jul 19, 2023
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
48 changes: 48 additions & 0 deletions solution/1200-1299/1229.Meeting Scheduler/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,54 @@ public:
};
```

### **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;
}
j += 1;
}
i += 1;
}

// The default is an empty vector
vec![]
}
}
```

### **Go**

```go
Expand Down
48 changes: 48 additions & 0 deletions solution/1200-1299/1229.Meeting Scheduler/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,54 @@ public:
};
```

### **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;
}
j += 1;
}
i += 1;
}

// The default is an empty vector
vec![]
}
}
```

### **Go**

```go
Expand Down
43 changes: 43 additions & 0 deletions solution/1200-1299/1229.Meeting Scheduler/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
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;
}
j += 1;
}
i += 1;
}

// The default is an empty vector
vec![]
}
}