From 7d9e1d464957c8cccb0ea13d3ee56853a1b4b99f Mon Sep 17 00:00:00 2001 From: BangDori Date: Wed, 11 Jun 2025 08:44:45 +0900 Subject: [PATCH 1/2] =?UTF-8?q?[=EA=B0=95=EB=B3=91=EC=A4=80]=20Course=20Sc?= =?UTF-8?q?hedule?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bangdori/207.js | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 bangdori/207.js diff --git a/bangdori/207.js b/bangdori/207.js new file mode 100644 index 0000000..9090520 --- /dev/null +++ b/bangdori/207.js @@ -0,0 +1,34 @@ +/** + * @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); + } + + while (queue.length) { + const curr = queue.pop(); + + for (const next of graph[curr]) { + indegree[next]--; + + if (indegree[next] === 0) { + queue.push(next); + } + } + } + + const isFinish = Math.max(...indegree) === 0; + return isFinish; +}; From d8aafeb7e69cc05fcdf28b3afd908f8946fdff1e Mon Sep 17 00:00:00 2001 From: BangDori Date: Wed, 11 Jun 2025 08:48:40 +0900 Subject: [PATCH 2/2] =?UTF-8?q?=EC=8B=9C=EA=B0=84=EB=B3=B5=EC=9E=A1?= =?UTF-8?q?=EB=8F=84=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bangdori/207.js | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/bangdori/207.js b/bangdori/207.js index 9090520..480eb1a 100644 --- a/bangdori/207.js +++ b/bangdori/207.js @@ -17,8 +17,10 @@ var canFinish = function (numCourses, prerequisites) { 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]--; @@ -29,6 +31,5 @@ var canFinish = function (numCourses, prerequisites) { } } - const isFinish = Math.max(...indegree) === 0; - return isFinish; + return numCourses === completed; };