|
| 1 | +# 207. Course Schedule |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Depth-first Search, Breadth-first Search, Graph, Topological Sort. |
| 5 | +- Similar Questions: Course Schedule II, Graph Valid Tree, Minimum Height Trees, Course Schedule III. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +There are a total of *n* courses you have to take, labeled from ```0``` to ```n-1```. |
| 10 | + |
| 11 | +Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: ```[0,1]``` |
| 12 | + |
| 13 | +Given the total number of courses and a list of prerequisite **pairs**, is it possible for you to finish all courses? |
| 14 | + |
| 15 | +**Example 1:** |
| 16 | + |
| 17 | +``` |
| 18 | +Input: 2, [[1,0]] |
| 19 | +Output: true |
| 20 | +Explanation: There are a total of 2 courses to take. |
| 21 | + To take course 1 you should have finished course 0. So it is possible. |
| 22 | +``` |
| 23 | + |
| 24 | +**Example 2:** |
| 25 | + |
| 26 | +``` |
| 27 | +Input: 2, [[1,0],[0,1]] |
| 28 | +Output: false |
| 29 | +Explanation: There are a total of 2 courses to take. |
| 30 | + To take course 1 you should have finished course 0, and to take course 0 you should |
| 31 | + also have finished course 1. So it is impossible. |
| 32 | +``` |
| 33 | + |
| 34 | +**Note:** |
| 35 | + |
| 36 | +- The input prerequisites is a graph represented by **a list of edges**, not adjacency matrices. Read more about how a graph is represented. |
| 37 | +- You may assume that there are no duplicate edges in the input prerequisites. |
| 38 | + |
| 39 | +## Solution |
| 40 | + |
| 41 | +```javascript |
| 42 | +/** |
| 43 | + * @param {number} numCourses |
| 44 | + * @param {number[][]} prerequisites |
| 45 | + * @return {boolean} |
| 46 | + */ |
| 47 | +var canFinish = function(numCourses, prerequisites) { |
| 48 | + var edges = Array(numCourses).fill(0).map(_ => Array(numCourses).fill(0)); |
| 49 | + var incoming = Array(numCourses).fill(0); |
| 50 | + var len = prerequisites.length; |
| 51 | + var post = 0; |
| 52 | + var prev = 0; |
| 53 | + var queue = []; |
| 54 | + var num = 0; |
| 55 | + var count = 0; |
| 56 | + |
| 57 | + for (var i = 0; i < len; i++) { |
| 58 | + prev = prerequisites[i][1]; |
| 59 | + post = prerequisites[i][0]; |
| 60 | + if (edges[prev][post] === 0) { |
| 61 | + incoming[post]++; |
| 62 | + edges[prev][post] = 1; |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + for (var j = 0; j < numCourses; j++) { |
| 67 | + if (incoming[j] === 0) queue.push(j); |
| 68 | + } |
| 69 | + |
| 70 | + while (queue.length) { |
| 71 | + count++; |
| 72 | + num = queue.pop() |
| 73 | + for (var k = 0; k < numCourses; k++) { |
| 74 | + if (edges[num][k] === 1 && --incoming[k] === 0) queue.push(k); |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + return count === numCourses; |
| 79 | +}; |
| 80 | +``` |
| 81 | + |
| 82 | +**Explain:** |
| 83 | + |
| 84 | +nope. |
| 85 | + |
| 86 | +**Complexity:** |
| 87 | + |
| 88 | +* Time complexity : O(n^2). |
| 89 | +* Space complexity : O(n^2). |
0 commit comments