|
| 1 | +# 210. Course Schedule II |
| 2 | + |
| 3 | +- Difficulty: Medium. |
| 4 | +- Related Topics: Depth-First Search, Breadth-First Search, Graph, Topological Sort. |
| 5 | +- Similar Questions: Course Schedule, Alien Dictionary, Minimum Height Trees, Sequence Reconstruction, Course Schedule III, Parallel Courses, Find All Possible Recipes from Given Supplies, Build a Matrix With Conditions, Sort Array by Moving Items to Empty Space. |
| 6 | + |
| 7 | +## Problem |
| 8 | + |
| 9 | +There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [ai, bi]` indicates that you **must** take course `bi` first if you want to take course `ai`. |
| 10 | + |
| 11 | + |
| 12 | + |
| 13 | +- For example, the pair `[0, 1]`, indicates that to take course `0` you have to first take course `1`. |
| 14 | + |
| 15 | + |
| 16 | +Return **the ordering of courses you should take to finish all courses**. If there are many valid answers, return **any** of them. If it is impossible to finish all courses, return **an empty array**. |
| 17 | + |
| 18 | + |
| 19 | +Example 1: |
| 20 | + |
| 21 | +``` |
| 22 | +Input: numCourses = 2, prerequisites = [[1,0]] |
| 23 | +Output: [0,1] |
| 24 | +Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1]. |
| 25 | +``` |
| 26 | + |
| 27 | +Example 2: |
| 28 | + |
| 29 | +``` |
| 30 | +Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]] |
| 31 | +Output: [0,2,1,3] |
| 32 | +Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0. |
| 33 | +So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3]. |
| 34 | +``` |
| 35 | + |
| 36 | +Example 3: |
| 37 | + |
| 38 | +``` |
| 39 | +Input: numCourses = 1, prerequisites = [] |
| 40 | +Output: [0] |
| 41 | +``` |
| 42 | + |
| 43 | + |
| 44 | +**Constraints:** |
| 45 | + |
| 46 | + |
| 47 | + |
| 48 | +- `1 <= numCourses <= 2000` |
| 49 | + |
| 50 | +- `0 <= prerequisites.length <= numCourses * (numCourses - 1)` |
| 51 | + |
| 52 | +- `prerequisites[i].length == 2` |
| 53 | + |
| 54 | +- `0 <= ai, bi < numCourses` |
| 55 | + |
| 56 | +- `ai != bi` |
| 57 | + |
| 58 | +- All the pairs `[ai, bi]` are **distinct**. |
| 59 | + |
| 60 | + |
| 61 | + |
| 62 | +## Solution |
| 63 | + |
| 64 | +```javascript |
| 65 | +/** |
| 66 | + * @param {number} numCourses |
| 67 | + * @param {number[][]} prerequisites |
| 68 | + * @return {number[]} |
| 69 | + */ |
| 70 | +var findOrder = function(numCourses, prerequisites) { |
| 71 | + var requiredByMap = Array(numCourses).fill(0).map(() => []); |
| 72 | + var requiringMap = Array(numCourses).fill(0); |
| 73 | + for (var i = 0; i < prerequisites.length; i++) { |
| 74 | + requiringMap[prerequisites[i][0]]++; |
| 75 | + requiredByMap[prerequisites[i][1]].push(prerequisites[i][0]); |
| 76 | + } |
| 77 | + var queue = new MinPriorityQueue(); |
| 78 | + for (var j = 0; j < numCourses; j++) { |
| 79 | + queue.enqueue(j, requiringMap[j]); |
| 80 | + } |
| 81 | + var res = []; |
| 82 | + while (queue.size()) { |
| 83 | + var item = queue.dequeue(); |
| 84 | + if (requiringMap[item.element] !== item.priority) continue; |
| 85 | + if (item.priority !== 0) return []; |
| 86 | + res.push(item.element); |
| 87 | + for (var k = 0; k < requiredByMap[item.element].length; k++) { |
| 88 | + requiringMap[requiredByMap[item.element][k]]--; |
| 89 | + queue.enqueue(requiredByMap[item.element][k], requiringMap[requiredByMap[item.element][k]]); |
| 90 | + } |
| 91 | + } |
| 92 | + return res; |
| 93 | +}; |
| 94 | +``` |
| 95 | + |
| 96 | +**Explain:** |
| 97 | + |
| 98 | +nope. |
| 99 | + |
| 100 | +**Complexity:** |
| 101 | + |
| 102 | +* Time complexity : O(n * log(n)). |
| 103 | +* Space complexity : O(n). |
0 commit comments