Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6104a75

Browse files
committedJun 6, 2025·
course schedule solution
1 parent f06e0ee commit 6104a75

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
 

‎course-schedule/moonjonghoo.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var canFinish = function (numCourses, prerequisites) {
2+
const graph = Array.from({ length: numCourses }, () => []);
3+
const inDegree = Array(numCourses).fill(0);
4+
5+
// 그래프 만들기 및 진입 차수 계산
6+
for (const [course, pre] of prerequisites) {
7+
graph[pre].push(course);
8+
inDegree[course]++;
9+
}
10+
11+
// 진입 차수가 0인 노드부터 시작
12+
const queue = [];
13+
for (let i = 0; i < numCourses; i++) {
14+
if (inDegree[i] === 0) queue.push(i);
15+
}
16+
17+
let count = 0;
18+
while (queue.length) {
19+
const node = queue.shift();
20+
count++;
21+
22+
for (const neighbor of graph[node]) {
23+
inDegree[neighbor]--;
24+
if (inDegree[neighbor] === 0) {
25+
queue.push(neighbor);
26+
}
27+
}
28+
}
29+
30+
return count === numCourses;
31+
};

0 commit comments

Comments
 (0)
Please sign in to comment.