From f4e4e71f191c3f7bdac076e26acc89b5a961a1bf Mon Sep 17 00:00:00 2001 From: Michael Xu Date: Sat, 22 Jul 2023 22:28:27 +0800 Subject: [PATCH] feat: add rust solution to lc problem: No.0207 --- .../0200-0299/0207.Course Schedule/README.md | 52 +++++++++++++++++++ .../0207.Course Schedule/README_EN.md | 52 +++++++++++++++++++ .../0207.Course Schedule/Solution.rs | 47 +++++++++++++++++ 3 files changed, 151 insertions(+) create mode 100644 solution/0200-0299/0207.Course Schedule/Solution.rs diff --git a/solution/0200-0299/0207.Course Schedule/README.md b/solution/0200-0299/0207.Course Schedule/README.md index 8f30283ba7787..8625297a33dc2 100644 --- a/solution/0200-0299/0207.Course Schedule/README.md +++ b/solution/0200-0299/0207.Course Schedule/README.md @@ -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>) -> bool { + let num_course = num_course as usize; + // The graph representation + let mut graph: Vec> = vec![vec![]; num_course]; + // Record the in degree for each node + let mut in_degree_vec: Vec = vec![0; num_course]; + let mut q: VecDeque = 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 diff --git a/solution/0200-0299/0207.Course Schedule/README_EN.md b/solution/0200-0299/0207.Course Schedule/README_EN.md index afa683330d236..feb5e2c3480e2 100644 --- a/solution/0200-0299/0207.Course Schedule/README_EN.md +++ b/solution/0200-0299/0207.Course Schedule/README_EN.md @@ -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>) -> bool { + let num_course = num_course as usize; + // The graph representation + let mut graph: Vec> = vec![vec![]; num_course]; + // Record the in degree for each node + let mut in_degree_vec: Vec = vec![0; num_course]; + let mut q: VecDeque = 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 diff --git a/solution/0200-0299/0207.Course Schedule/Solution.rs b/solution/0200-0299/0207.Course Schedule/Solution.rs new file mode 100644 index 0000000000000..c2dfb6ee7158c --- /dev/null +++ b/solution/0200-0299/0207.Course Schedule/Solution.rs @@ -0,0 +1,47 @@ +use std::collections::VecDeque; + +impl Solution { + #[allow(dead_code)] + pub fn can_finish(num_course: i32, prerequisites: Vec>) -> bool { + let num_course = num_course as usize; + // The graph representation + let mut graph: Vec> = vec![vec![]; num_course]; + // Record the in degree for each node + let mut in_degree_vec: Vec = vec![0; num_course]; + let mut q: VecDeque = 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 + } +} \ No newline at end of file