Skip to content

Commit 5204794

Browse files
authoredJul 23, 2023
feat: add rust solution to lc problem: No.0207 (#1271)
1 parent c726108 commit 5204794

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed
 

‎solution/0200-0299/0207.Course Schedule/README.md

+52
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,58 @@ public:
155155
};
156156
```
157157
158+
### **Rust**
159+
160+
```rust
161+
use std::collections::VecDeque;
162+
163+
impl Solution {
164+
#[allow(dead_code)]
165+
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
166+
let num_course = num_course as usize;
167+
// The graph representation
168+
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
169+
// Record the in degree for each node
170+
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
171+
let mut q: VecDeque<usize> = VecDeque::new();
172+
let mut count = 0;
173+
174+
// Initialize the graph & in degree vector
175+
for p in &prerequisites {
176+
let (from, to) = (p[0], p[1]);
177+
graph[from as usize].push(to);
178+
in_degree_vec[to as usize] += 1;
179+
}
180+
181+
// Enqueue the first batch of nodes with in degree 0
182+
for i in 0..num_course {
183+
if in_degree_vec[i] == 0 {
184+
q.push_back(i);
185+
}
186+
}
187+
188+
// Begin the traverse & update through the graph
189+
while !q.is_empty() {
190+
// Get the current node index
191+
let index = q.front().unwrap().clone();
192+
// This course can be finished
193+
count += 1;
194+
q.pop_front();
195+
for i in &graph[index] {
196+
// Update the in degree for the current node
197+
in_degree_vec[*i as usize] -= 1;
198+
// See if can be enqueued
199+
if in_degree_vec[*i as usize] == 0 {
200+
q.push_back(*i as usize);
201+
}
202+
}
203+
}
204+
205+
count == num_course
206+
}
207+
}
208+
```
209+
158210
### **Go**
159211

160212
```go

‎solution/0200-0299/0207.Course Schedule/README_EN.md

+52
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,58 @@ public:
137137
};
138138
```
139139
140+
### **Rust**
141+
142+
```rust
143+
use std::collections::VecDeque;
144+
145+
impl Solution {
146+
#[allow(dead_code)]
147+
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
148+
let num_course = num_course as usize;
149+
// The graph representation
150+
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
151+
// Record the in degree for each node
152+
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
153+
let mut q: VecDeque<usize> = VecDeque::new();
154+
let mut count = 0;
155+
156+
// Initialize the graph & in degree vector
157+
for p in &prerequisites {
158+
let (from, to) = (p[0], p[1]);
159+
graph[from as usize].push(to);
160+
in_degree_vec[to as usize] += 1;
161+
}
162+
163+
// Enqueue the first batch of nodes with in degree 0
164+
for i in 0..num_course {
165+
if in_degree_vec[i] == 0 {
166+
q.push_back(i);
167+
}
168+
}
169+
170+
// Begin the traverse & update through the graph
171+
while !q.is_empty() {
172+
// Get the current node index
173+
let index = q.front().unwrap().clone();
174+
// This course can be finished
175+
count += 1;
176+
q.pop_front();
177+
for i in &graph[index] {
178+
// Update the in degree for the current node
179+
in_degree_vec[*i as usize] -= 1;
180+
// See if can be enqueued
181+
if in_degree_vec[*i as usize] == 0 {
182+
q.push_back(*i as usize);
183+
}
184+
}
185+
}
186+
187+
count == num_course
188+
}
189+
}
190+
```
191+
140192
### **Go**
141193

142194
```go
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
use std::collections::VecDeque;
2+
3+
impl Solution {
4+
#[allow(dead_code)]
5+
pub fn can_finish(num_course: i32, prerequisites: Vec<Vec<i32>>) -> bool {
6+
let num_course = num_course as usize;
7+
// The graph representation
8+
let mut graph: Vec<Vec<i32>> = vec![vec![]; num_course];
9+
// Record the in degree for each node
10+
let mut in_degree_vec: Vec<i32> = vec![0; num_course];
11+
let mut q: VecDeque<usize> = VecDeque::new();
12+
let mut count = 0;
13+
14+
// Initialize the graph & in degree vector
15+
for p in &prerequisites {
16+
let (from, to) = (p[0], p[1]);
17+
graph[from as usize].push(to);
18+
in_degree_vec[to as usize] += 1;
19+
}
20+
21+
// Enqueue the first batch of nodes with in degree 0
22+
for i in 0..num_course {
23+
if in_degree_vec[i] == 0 {
24+
q.push_back(i);
25+
}
26+
}
27+
28+
// Begin the traverse & update through the graph
29+
while !q.is_empty() {
30+
// Get the current node index
31+
let index = q.front().unwrap().clone();
32+
// This course can be finished
33+
count += 1;
34+
q.pop_front();
35+
for i in &graph[index] {
36+
// Update the in degree for the current node
37+
in_degree_vec[*i as usize] -= 1;
38+
// See if can be enqueued
39+
if in_degree_vec[*i as usize] == 0 {
40+
q.push_back(*i as usize);
41+
}
42+
}
43+
}
44+
45+
count == num_course
46+
}
47+
}

0 commit comments

Comments
 (0)