Skip to content

Commit

Permalink
feat: add solutions to lc problem: No.0207 (#3915)
Browse files Browse the repository at this point in the history
No.0207.Course Schedule
  • Loading branch information
yanglbme authored Jan 1, 2025
1 parent 44960a4 commit ea6a6b0
Show file tree
Hide file tree
Showing 9 changed files with 125 additions and 184 deletions.
97 changes: 39 additions & 58 deletions solution/0200-0299/0207.Course Schedule/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
}
```
Expand All @@ -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;
}
};
```
Expand All @@ -183,49 +179,46 @@ 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 {
q = append(q, j)
}
}
}
return cnt == numCourses
return numCourses == 0
}
```

#### TypeScript

```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;
}
```

Expand All @@ -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<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;
pub fn can_finish(mut num_courses: i32, prerequisites: Vec<Vec<i32>>) -> bool {
let mut g: Vec<Vec<i32>> = vec![vec![]; num_courses as usize];
let mut indeg: Vec<i32> = 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<usize> = 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
}
}
```
Expand Down
Loading

0 comments on commit ea6a6b0

Please sign in to comment.