Skip to content

Commit aedd536

Browse files
committed
✨ [207] Course Schedule - topological sort
1 parent 1b4c426 commit aedd536

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

207/my_solution.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/**
2+
* @param {number} numCourses
3+
* @param {number[][]} prerequisites
4+
* @return {boolean}
5+
*/
6+
const canFinish = function (numCourses, prerequisites) {
7+
// Step 1: Build the graph (adjacency list)
8+
// console.log(graph)
9+
const graph = buildGraph(numCourses, prerequisites);
10+
console.log("graph (adjacency list)")
11+
console.log(graph)
12+
13+
// Step 2: Perform topological sort
14+
const inDegree = new Array(numCourses).fill(0);
15+
for (let [course, pre] of prerequisites) {
16+
inDegree[course]++;
17+
}
18+
console.log("inDegree")
19+
console.log(inDegree)
20+
21+
const queue = [];
22+
for (let i = 0; i < numCourses; i++) {
23+
if (inDegree[i] === 0) {
24+
queue.push(i);
25+
}
26+
}
27+
28+
let count = 0;
29+
while (queue.length > 0) {
30+
const course = queue.shift();
31+
count++;
32+
33+
for (const neighbor of graph[course]) {
34+
inDegree[neighbor]--;
35+
if (inDegree[neighbor] === 0) {
36+
queue.push(neighbor);
37+
}
38+
}
39+
}
40+
41+
// Step 3: Check the count
42+
return count === numCourses;
43+
};
44+
45+
// Helper function to build the graph
46+
function buildGraph(numCourses, prerequisites) {
47+
const graph = new Array(numCourses).fill().map(() => []);
48+
for (const [course, prerequisite] of prerequisites) {
49+
graph[prerequisite].push(course);
50+
}
51+
return graph;
52+
}
53+
54+
console.log(canFinish(2, [[1, 0]])); // Output: true
55+
console.log(canFinish(2, [[1, 0], [0, 1]])); // Output: false

207/solution.js

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/**
2+
* @param {number} numCourses
3+
* @param {number[][]} prerequisites
4+
* @return {boolean}
5+
*/
6+
const canFinish = function (numCourses, prerequisites) {
7+
// Step 1: Build the graph (adjacency list)
8+
const graph = buildGraph(numCourses, prerequisites);
9+
10+
// Step 2: Perform topological sort
11+
const inDegree = new Array(numCourses).fill(0);
12+
for (let [course, pre] of prerequisites) {
13+
inDegree[course]++;
14+
}
15+
16+
const queue = [];
17+
for (let i = 0; i < numCourses; i++) {
18+
if (inDegree[i] === 0) {
19+
queue.push(i);
20+
}
21+
}
22+
23+
let count = 0;
24+
while (queue.length > 0) {
25+
const course = queue.shift();
26+
count++;
27+
28+
for (const neighbor of graph[course]) {
29+
inDegree[neighbor]--;
30+
if (inDegree[neighbor] === 0) {
31+
queue.push(neighbor);
32+
}
33+
}
34+
}
35+
36+
// Step 3: Check the count
37+
return count === numCourses;
38+
};
39+
40+
// Helper function to build the graph
41+
function buildGraph(numCourses, prerequisites) {
42+
const graph = new Array(numCourses).fill().map(() => []);
43+
for (const [course, prerequisite] of prerequisites) {
44+
graph[prerequisite].push(course);
45+
}
46+
return graph;
47+
}
48+
49+
console.log(canFinish(2, [[1, 0]])); // Output: true
50+
console.log(canFinish(2, [[1, 0], [0, 1]])); // Output: false

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
- [198. House Robber](./198/)
4444
- [200. Number of Islands](./200/)
4545
- [206. Reverse Linked List](./206/)
46+
- [207. Course Schedule](./207/)
4647
- [213. House Robber II](./213/)
4748
- [217. Contains Duplicate](./217/)
4849
- [238. Product of Array Except Self](./238/)
@@ -105,7 +106,7 @@ Batch create:
105106
NOTE: JS IS HERE
106107
-->
107108
```ssh
108-
chapter=435 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
109+
chapter=207 && mkdir ./$chapter && touch ./$chapter/my_solution.js && touch ./$chapter/solution.js && alias x="node ./$chapter/my_solution.js"
109110
```
110111
> then you can use `x` for quick debug.
111112

test.js

+3
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,6 @@ console.log(o)
5555
let x = Array(3).fill([]);
5656
x[1].push(2)
5757
console.log(x)
58+
59+
const g = Array(3).fill().map(() => []);
60+
console.log(g)

0 commit comments

Comments
 (0)