diff --git a/bangdori/207.js b/bangdori/207.js new file mode 100644 index 0000000..480eb1a --- /dev/null +++ b/bangdori/207.js @@ -0,0 +1,35 @@ +/** + * @param {number} numCourses + * @param {number[][]} prerequisites + * @return {boolean} + */ +var canFinish = function (numCourses, prerequisites) { + const graph = Array.from({ length: numCourses }, () => []); + const indegree = Array(numCourses).fill(0); + + for (const [a, b] of prerequisites) { + graph[b].push(a); + indegree[a]++; + } + + const queue = []; + for (let i = 0; i < numCourses; i++) { + if (indegree[i] === 0) queue.push(i); + } + + let completed = 0; + while (queue.length) { + const curr = queue.pop(); + completed++; + + for (const next of graph[curr]) { + indegree[next]--; + + if (indegree[next] === 0) { + queue.push(next); + } + } + } + + return numCourses === completed; +};