Skip to content
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

feat: add rust solution to lc problem: No.0207 #1271

Merged
merged 2 commits into from
Jul 23, 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
52 changes: 52 additions & 0 deletions solution/0200-0299/0207.Course Schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,58 @@ public:
};
```

### **Rust**

```rust
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
let num_course = num_course as usize;
// The graph representation
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
// Record the in degree for each node
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
let mut q: VecDeque<usize> = VecDeque::new();
let mut count = 0;

// Initialize the graph & in degree vector
for p in &prerequisites {
let (from, to) = (p[0], p[1]);
graph[from as usize].push(to);
in_degree_vec[to as usize] += 1;
}

// Enqueue the first batch of nodes with in degree 0
for i in 0..num_course {
if in_degree_vec[i] == 0 {
q.push_back(i);
}
}

// Begin the traverse & update through the graph
while !q.is_empty() {
// Get the current node index
let index = q.front().unwrap().clone();
// This course can be finished
count += 1;
q.pop_front();
for i in &graph[index] {
// Update the in degree for the current node
in_degree_vec[*i as usize] -= 1;
// See if can be enqueued
if in_degree_vec[*i as usize] == 0 {
q.push_back(*i as usize);
}
}
}

count == num_course
}
}
```

### **Go**

```go
Expand Down
52 changes: 52 additions & 0 deletions solution/0200-0299/0207.Course Schedule/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,58 @@ public:
};
```

### **Rust**

```rust
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
let num_course = num_course as usize;
// The graph representation
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
// Record the in degree for each node
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
let mut q: VecDeque<usize> = VecDeque::new();
let mut count = 0;

// Initialize the graph & in degree vector
for p in &prerequisites {
let (from, to) = (p[0], p[1]);
graph[from as usize].push(to);
in_degree_vec[to as usize] += 1;
}

// Enqueue the first batch of nodes with in degree 0
for i in 0..num_course {
if in_degree_vec[i] == 0 {
q.push_back(i);
}
}

// Begin the traverse & update through the graph
while !q.is_empty() {
// Get the current node index
let index = q.front().unwrap().clone();
// This course can be finished
count += 1;
q.pop_front();
for i in &graph[index] {
// Update the in degree for the current node
in_degree_vec[*i as usize] -= 1;
// See if can be enqueued
if in_degree_vec[*i as usize] == 0 {
q.push_back(*i as usize);
}
}
}

count == num_course
}
}
```

### **Go**

```go
Expand Down
47 changes: 47 additions & 0 deletions solution/0200-0299/0207.Course Schedule/Solution.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use std::collections::VecDeque;

impl Solution {
#[allow(dead_code)]
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
let num_course = num_course as usize;
// The graph representation
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
// Record the in degree for each node
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
let mut q: VecDeque<usize> = VecDeque::new();
let mut count = 0;

// Initialize the graph & in degree vector
for p in &prerequisites {
let (from, to) = (p[0], p[1]);
graph[from as usize].push(to);
in_degree_vec[to as usize] += 1;
}

// Enqueue the first batch of nodes with in degree 0
for i in 0..num_course {
if in_degree_vec[i] == 0 {
q.push_back(i);
}
}

// Begin the traverse & update through the graph
while !q.is_empty() {
// Get the current node index
let index = q.front().unwrap().clone();
// This course can be finished
count += 1;
q.pop_front();
for i in &graph[index] {
// Update the in degree for the current node
in_degree_vec[*i as usize] -= 1;
// See if can be enqueued
if in_degree_vec[*i as usize] == 0 {
q.push_back(*i as usize);
}
}
}

count == num_course
}
}