diff --git a/solution/0200-0299/0207.Course Schedule/README.md b/solution/0200-0299/0207.Course Schedule/README.md index 109baf1ef298c..3c1b02a563716 100644 --- a/solution/0200-0299/0207.Course Schedule/README.md +++ b/solution/0200-0299/0207.Course Schedule/README.md @@ -80,21 +80,19 @@ tags: ```python class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: - g = defaultdict(list) + g = [[] for _ in range(numCourses)] indeg = [0] * numCourses for a, b in prerequisites: g[b].append(a) indeg[a] += 1 - cnt = 0 - q = deque(i for i, x in enumerate(indeg) if x == 0) - while q: - i = q.popleft() - cnt += 1 + q = [i for i, x in enumerate(indeg) if x == 0] + for i in q: + numCourses -= 1 for j in g[i]: indeg[j] -= 1 if indeg[j] == 0: q.append(j) - return cnt == numCourses + return numCourses == 0 ``` #### Java @@ -116,17 +114,16 @@ class Solution { q.offer(i); } } - int cnt = 0; while (!q.isEmpty()) { int i = q.poll(); - ++cnt; + --numCourses; for (int j : g[i]) { if (--indeg[j] == 0) { q.offer(j); } } } - return cnt == numCourses; + return numCourses == 0; } } ``` @@ -150,18 +147,17 @@ public: q.push(i); } } - int cnt = 0; while (!q.empty()) { int i = q.front(); q.pop(); - ++cnt; + --numCourses; for (int j : g[i]) { if (--indeg[j] == 0) { q.push(j); } } } - return cnt == numCourses; + return numCourses == 0; } }; ``` @@ -183,11 +179,10 @@ func canFinish(numCourses int, prerequisites [][]int) bool { q = append(q, i) } } - cnt := 0 for len(q) > 0 { i := q[0] q = q[1:] - cnt++ + numCourses-- for _, j := range g[i] { indeg[j]-- if indeg[j] == 0 { @@ -195,7 +190,7 @@ func canFinish(numCourses int, prerequisites [][]int) bool { } } } - return cnt == numCourses + return numCourses == 0 } ``` @@ -203,29 +198,27 @@ func canFinish(numCourses int, prerequisites [][]int) bool { ```ts function canFinish(numCourses: number, prerequisites: number[][]): boolean { - const g: number[][] = new Array(numCourses).fill(0).map(() => []); - const indeg: number[] = new Array(numCourses).fill(0); + const g: number[][] = Array.from({ length: numCourses }, () => []); + const indeg: number[] = Array(numCourses).fill(0); for (const [a, b] of prerequisites) { g[b].push(a); indeg[a]++; } const q: number[] = []; for (let i = 0; i < numCourses; ++i) { - if (indeg[i] == 0) { + if (indeg[i] === 0) { q.push(i); } } - let cnt = 0; - while (q.length) { - const i = q.shift()!; - cnt++; + for (const i of q) { + --numCourses; for (const j of g[i]) { - if (--indeg[j] == 0) { + if (--indeg[j] === 0) { q.push(j); } } } - return cnt == numCourses; + return numCourses === 0; } ``` @@ -235,48 +228,36 @@ function canFinish(numCourses: number, prerequisites: number[][]): boolean { 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; + pub fn can_finish(mut num_courses: i32, prerequisites: Vec>) -> bool { + let mut g: Vec> = vec![vec![]; num_courses as usize]; + let mut indeg: Vec = vec![0; num_courses as usize]; + + for p in prerequisites { + let a = p[0] as usize; + let b = p[1] as usize; + g[b].push(a as i32); + indeg[a] += 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); + let mut q: VecDeque = VecDeque::new(); + for i in 0..num_courses { + if indeg[i as usize] == 0 { + q.push_back(i as usize); } } - // 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); + while let Some(i) = q.pop_front() { + num_courses -= 1; + for &j in &g[i] { + let j = j as usize; + indeg[j] -= 1; + if indeg[j] == 0 { + q.push_back(j); } } } - count == num_course + num_courses == 0 } } ``` diff --git a/solution/0200-0299/0207.Course Schedule/README_EN.md b/solution/0200-0299/0207.Course Schedule/README_EN.md index 174efc4c382b6..c18d8ad2c1ab6 100644 --- a/solution/0200-0299/0207.Course Schedule/README_EN.md +++ b/solution/0200-0299/0207.Course Schedule/README_EN.md @@ -33,7 +33,7 @@ tags:
 Input: numCourses = 2, prerequisites = [[1,0]]
 Output: true
-Explanation: There are a total of 2 courses to take. 
+Explanation: There are a total of 2 courses to take.
 To take course 1 you should have finished course 0. So it is possible.
 
@@ -42,7 +42,7 @@ To take course 1 you should have finished course 0. So it is possible.
 Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
 Output: false
-Explanation: There are a total of 2 courses to take. 
+Explanation: There are a total of 2 courses to take.
 To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
 
@@ -80,21 +80,19 @@ The time complexity is $O(n + m)$, and the space complexity is $O(n + m)$. Here, ```python class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: - g = defaultdict(list) + g = [[] for _ in range(numCourses)] indeg = [0] * numCourses for a, b in prerequisites: g[b].append(a) indeg[a] += 1 - cnt = 0 - q = deque(i for i, x in enumerate(indeg) if x == 0) - while q: - i = q.popleft() - cnt += 1 + q = [i for i, x in enumerate(indeg) if x == 0] + for i in q: + numCourses -= 1 for j in g[i]: indeg[j] -= 1 if indeg[j] == 0: q.append(j) - return cnt == numCourses + return numCourses == 0 ``` #### Java @@ -116,17 +114,16 @@ class Solution { q.offer(i); } } - int cnt = 0; while (!q.isEmpty()) { int i = q.poll(); - ++cnt; + --numCourses; for (int j : g[i]) { if (--indeg[j] == 0) { q.offer(j); } } } - return cnt == numCourses; + return numCourses == 0; } } ``` @@ -150,18 +147,17 @@ public: q.push(i); } } - int cnt = 0; while (!q.empty()) { int i = q.front(); q.pop(); - ++cnt; + --numCourses; for (int j : g[i]) { if (--indeg[j] == 0) { q.push(j); } } } - return cnt == numCourses; + return numCourses == 0; } }; ``` @@ -183,11 +179,10 @@ func canFinish(numCourses int, prerequisites [][]int) bool { q = append(q, i) } } - cnt := 0 for len(q) > 0 { i := q[0] q = q[1:] - cnt++ + numCourses-- for _, j := range g[i] { indeg[j]-- if indeg[j] == 0 { @@ -195,7 +190,7 @@ func canFinish(numCourses int, prerequisites [][]int) bool { } } } - return cnt == numCourses + return numCourses == 0 } ``` @@ -203,29 +198,27 @@ func canFinish(numCourses int, prerequisites [][]int) bool { ```ts function canFinish(numCourses: number, prerequisites: number[][]): boolean { - const g: number[][] = new Array(numCourses).fill(0).map(() => []); - const indeg: number[] = new Array(numCourses).fill(0); + const g: number[][] = Array.from({ length: numCourses }, () => []); + const indeg: number[] = Array(numCourses).fill(0); for (const [a, b] of prerequisites) { g[b].push(a); indeg[a]++; } const q: number[] = []; for (let i = 0; i < numCourses; ++i) { - if (indeg[i] == 0) { + if (indeg[i] === 0) { q.push(i); } } - let cnt = 0; - while (q.length) { - const i = q.shift()!; - cnt++; + for (const i of q) { + --numCourses; for (const j of g[i]) { - if (--indeg[j] == 0) { + if (--indeg[j] === 0) { q.push(j); } } } - return cnt == numCourses; + return numCourses === 0; } ``` @@ -235,48 +228,36 @@ function canFinish(numCourses: number, prerequisites: number[][]): boolean { 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; + pub fn can_finish(mut num_courses: i32, prerequisites: Vec>) -> bool { + let mut g: Vec> = vec![vec![]; num_courses as usize]; + let mut indeg: Vec = vec![0; num_courses as usize]; + + for p in prerequisites { + let a = p[0] as usize; + let b = p[1] as usize; + g[b].push(a as i32); + indeg[a] += 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); + let mut q: VecDeque = VecDeque::new(); + for i in 0..num_courses { + if indeg[i as usize] == 0 { + q.push_back(i as usize); } } - // 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); + while let Some(i) = q.pop_front() { + num_courses -= 1; + for &j in &g[i] { + let j = j as usize; + indeg[j] -= 1; + if indeg[j] == 0 { + q.push_back(j); } } } - count == num_course + num_courses == 0 } } ``` @@ -302,17 +283,16 @@ public class Solution { q.Enqueue(i); } } - var cnt = 0; while (q.Count > 0) { int i = q.Dequeue(); - ++cnt; + --numCourses; foreach (int j in g[i]) { if (--indeg[j] == 0) { q.Enqueue(j); } } } - return cnt == numCourses; + return numCourses == 0; } } ``` diff --git a/solution/0200-0299/0207.Course Schedule/Solution.cpp b/solution/0200-0299/0207.Course Schedule/Solution.cpp index 9251c69502002..fc70738800a7f 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.cpp +++ b/solution/0200-0299/0207.Course Schedule/Solution.cpp @@ -14,17 +14,16 @@ class Solution { q.push(i); } } - int cnt = 0; while (!q.empty()) { int i = q.front(); q.pop(); - ++cnt; + --numCourses; for (int j : g[i]) { if (--indeg[j] == 0) { q.push(j); } } } - return cnt == numCourses; + return numCourses == 0; } -}; \ No newline at end of file +}; diff --git a/solution/0200-0299/0207.Course Schedule/Solution.cs b/solution/0200-0299/0207.Course Schedule/Solution.cs index 9710ea6b87bfe..dbfa56c7b8b96 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.cs +++ b/solution/0200-0299/0207.Course Schedule/Solution.cs @@ -16,16 +16,15 @@ public bool CanFinish(int numCourses, int[][] prerequisites) { q.Enqueue(i); } } - var cnt = 0; while (q.Count > 0) { int i = q.Dequeue(); - ++cnt; + --numCourses; foreach (int j in g[i]) { if (--indeg[j] == 0) { q.Enqueue(j); } } } - return cnt == numCourses; + return numCourses == 0; } } diff --git a/solution/0200-0299/0207.Course Schedule/Solution.go b/solution/0200-0299/0207.Course Schedule/Solution.go index f76b3b7bc4902..53e26f56aae66 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.go +++ b/solution/0200-0299/0207.Course Schedule/Solution.go @@ -12,11 +12,10 @@ func canFinish(numCourses int, prerequisites [][]int) bool { q = append(q, i) } } - cnt := 0 for len(q) > 0 { i := q[0] q = q[1:] - cnt++ + numCourses-- for _, j := range g[i] { indeg[j]-- if indeg[j] == 0 { @@ -24,5 +23,5 @@ func canFinish(numCourses int, prerequisites [][]int) bool { } } } - return cnt == numCourses -} \ No newline at end of file + return numCourses == 0 +} diff --git a/solution/0200-0299/0207.Course Schedule/Solution.java b/solution/0200-0299/0207.Course Schedule/Solution.java index 17972d69258f5..358e15abbf64a 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.java +++ b/solution/0200-0299/0207.Course Schedule/Solution.java @@ -14,16 +14,15 @@ public boolean canFinish(int numCourses, int[][] prerequisites) { q.offer(i); } } - int cnt = 0; while (!q.isEmpty()) { int i = q.poll(); - ++cnt; + --numCourses; for (int j : g[i]) { if (--indeg[j] == 0) { q.offer(j); } } } - return cnt == numCourses; + return numCourses == 0; } -} \ No newline at end of file +} diff --git a/solution/0200-0299/0207.Course Schedule/Solution.py b/solution/0200-0299/0207.Course Schedule/Solution.py index 917d6f03827c8..ec692bfd9bc70 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.py +++ b/solution/0200-0299/0207.Course Schedule/Solution.py @@ -1,17 +1,15 @@ class Solution: def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool: - g = defaultdict(list) + g = [[] for _ in range(numCourses)] indeg = [0] * numCourses for a, b in prerequisites: g[b].append(a) indeg[a] += 1 - cnt = 0 - q = deque(i for i, x in enumerate(indeg) if x == 0) - while q: - i = q.popleft() - cnt += 1 + q = [i for i, x in enumerate(indeg) if x == 0] + for i in q: + numCourses -= 1 for j in g[i]: indeg[j] -= 1 if indeg[j] == 0: q.append(j) - return cnt == numCourses + return numCourses == 0 diff --git a/solution/0200-0299/0207.Course Schedule/Solution.rs b/solution/0200-0299/0207.Course Schedule/Solution.rs index a23d4806b04e2..4a1f19a637fb5 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.rs +++ b/solution/0200-0299/0207.Course Schedule/Solution.rs @@ -1,47 +1,35 @@ 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; + pub fn can_finish(mut num_courses: i32, prerequisites: Vec>) -> bool { + let mut g: Vec> = vec![vec![]; num_courses as usize]; + let mut indeg: Vec = vec![0; num_courses as usize]; - // 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; + for p in prerequisites { + let a = p[0] as usize; + let b = p[1] as usize; + g[b].push(a as i32); + indeg[a] += 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); + let mut q: VecDeque = VecDeque::new(); + for i in 0..num_courses { + if indeg[i as usize] == 0 { + q.push_back(i as usize); } } - // 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); + while let Some(i) = q.pop_front() { + num_courses -= 1; + for &j in &g[i] { + let j = j as usize; + indeg[j] -= 1; + if indeg[j] == 0 { + q.push_back(j); } } } - count == num_course + num_courses == 0 } } diff --git a/solution/0200-0299/0207.Course Schedule/Solution.ts b/solution/0200-0299/0207.Course Schedule/Solution.ts index 55db7895b40e2..096cfdbcf89b2 100644 --- a/solution/0200-0299/0207.Course Schedule/Solution.ts +++ b/solution/0200-0299/0207.Course Schedule/Solution.ts @@ -1,25 +1,23 @@ function canFinish(numCourses: number, prerequisites: number[][]): boolean { - const g: number[][] = new Array(numCourses).fill(0).map(() => []); - const indeg: number[] = new Array(numCourses).fill(0); + const g: number[][] = Array.from({ length: numCourses }, () => []); + const indeg: number[] = Array(numCourses).fill(0); for (const [a, b] of prerequisites) { g[b].push(a); indeg[a]++; } const q: number[] = []; for (let i = 0; i < numCourses; ++i) { - if (indeg[i] == 0) { + if (indeg[i] === 0) { q.push(i); } } - let cnt = 0; - while (q.length) { - const i = q.shift()!; - cnt++; + for (const i of q) { + --numCourses; for (const j of g[i]) { - if (--indeg[j] == 0) { + if (--indeg[j] === 0) { q.push(j); } } } - return cnt == numCourses; + return numCourses === 0; }