|
| 1 | +/* |
| 2 | +
|
| 3 | +
|
| 4 | +-* Course Schedule *- |
| 5 | +
|
| 6 | +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. |
| 7 | +
|
| 8 | +For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1. |
| 9 | +Return true if you can finish all courses. Otherwise, return false. |
| 10 | +
|
| 11 | + |
| 12 | +
|
| 13 | +Example 1: |
| 14 | +
|
| 15 | +Input: numCourses = 2, prerequisites = [[1,0]] |
| 16 | +Output: true |
| 17 | +Explanation: There are a total of 2 courses to take. |
| 18 | +To take course 1 you should have finished course 0. So it is possible. |
| 19 | +Example 2: |
| 20 | +
|
| 21 | +Input: numCourses = 2, prerequisites = [[1,0],[0,1]] |
| 22 | +Output: false |
| 23 | +Explanation: There are a total of 2 courses to take. |
| 24 | +To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible. |
| 25 | + |
| 26 | +
|
| 27 | +Constraints: |
| 28 | +
|
| 29 | +1 <= numCourses <= 2000 |
| 30 | +0 <= prerequisites.length <= 5000 |
| 31 | +prerequisites[i].length == 2 |
| 32 | +0 <= ai, bi < numCourses |
| 33 | +All the pairs prerequisites[i] are unique. |
| 34 | +
|
| 35 | +
|
| 36 | +
|
| 37 | +*/ |
| 38 | + |
| 39 | +// Topological Sort |
| 40 | +// class Solution { |
| 41 | +// bool canFinish(int numCourses, List<List<int>> prerequisites) {} |
| 42 | +// } |
| 43 | + |
| 44 | +import 'dart:collection'; |
| 45 | + |
| 46 | +class A { |
| 47 | + bool canFinish(int numCourses, List<List<int>> prerequisites) { |
| 48 | + final List<int> answer = []; |
| 49 | + final Map<int, List<int>> map = Map(); |
| 50 | + final List<int> result = List<int>.filled(numCourses, 0); |
| 51 | + |
| 52 | + for (final List<int> x in prerequisites) { |
| 53 | + map[x[1]] = [...map[x[1]] ?? [], x[0]]; |
| 54 | + result[x[0]]++; |
| 55 | + } |
| 56 | + |
| 57 | + final Queue<int> q = Queue<int>(); |
| 58 | + for (int i = 0; i < numCourses; i++) { |
| 59 | + if (result[i] == 0) { |
| 60 | + q.add(i); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + while (q.isNotEmpty) { |
| 65 | + final int fr = q.removeFirst(); |
| 66 | + answer.add(fr); |
| 67 | + |
| 68 | + for (final int x in map[fr] ?? []) { |
| 69 | + result[x]--; |
| 70 | + if (result[x] == 0) { |
| 71 | + q.add(x); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + return answer.length == numCourses; |
| 77 | + } |
| 78 | +} |
| 79 | + |
| 80 | +// Union Find - Wrong |
| 81 | + |
| 82 | +class F { |
| 83 | + bool canFinish(int numCourses, List<List<int>> prerequisites) { |
| 84 | + List<int> par = List<int>.generate(numCourses, (index) => index); |
| 85 | + List<int> rank = List<int>.filled(numCourses, 0); |
| 86 | + |
| 87 | + for (var pre in prerequisites) { |
| 88 | + int x = pre[0]; |
| 89 | + int y = pre[1]; |
| 90 | + if (find(par, x) == find(par, y)) { |
| 91 | + return false; // Cycle detected |
| 92 | + } |
| 93 | + union(par, rank, x, y); |
| 94 | + } |
| 95 | + |
| 96 | + return true; |
| 97 | + } |
| 98 | + |
| 99 | + int find(List<int> par, int x) { |
| 100 | + if (par[x] != x) { |
| 101 | + par[x] = find(par, par[x]); |
| 102 | + } |
| 103 | + return par[x]; |
| 104 | + } |
| 105 | + |
| 106 | + void union(List<int> par, List<int> rank, int x, int y) { |
| 107 | + int rootX = find(par, x); |
| 108 | + int rootY = find(par, y); |
| 109 | + |
| 110 | + if (rootX != rootY) { |
| 111 | + if (rank[rootX] < rank[rootY]) { |
| 112 | + par[rootX] = rootY; |
| 113 | + } else if (rank[rootX] > rank[rootY]) { |
| 114 | + par[rootY] = rootX; |
| 115 | + } else { |
| 116 | + par[rootY] = rootX; |
| 117 | + rank[rootX]++; |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | +} |
| 122 | + |
| 123 | +// Correct |
| 124 | + |
| 125 | +class B { |
| 126 | + bool canFinish(int numCourses, List<List<int>> prerequisites) { |
| 127 | + final List<List<int>> graph = List<List<int>>.generate(numCourses, (_) => []); |
| 128 | + for (var pre in prerequisites) { |
| 129 | + final int x = pre[0]; |
| 130 | + final int y = pre[1]; |
| 131 | + graph[x].add(y); |
| 132 | + } |
| 133 | + |
| 134 | + final List<int> visited = List<int>.filled(numCourses, 0); |
| 135 | + for (int i = 0; i < numCourses; i++) { |
| 136 | + if (hasCycle(graph, visited, i)) { |
| 137 | + return false; // Cycle detected |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + return true; |
| 142 | + } |
| 143 | + |
| 144 | + bool hasCycle(List<List<int>> graph, List<int> visited, int node) { |
| 145 | + if (visited[node] == 1) { |
| 146 | + return true; // Cycle detected |
| 147 | + } |
| 148 | + if (visited[node] == -1) { |
| 149 | + return false; // Already visited and no cycle |
| 150 | + } |
| 151 | + |
| 152 | + visited[node] = 1; // Mark node as visited |
| 153 | + |
| 154 | + for (final int neighbor in graph[node]) { |
| 155 | + if (hasCycle(graph, visited, neighbor)) { |
| 156 | + return true; // Cycle detected |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + visited[node] = -1; // Mark node as visited and no cycle |
| 161 | + return false; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +class Solution { |
| 166 | + bool canFinish(int numCourses, List<List<int>> prerequisites) { |
| 167 | + final List<List<int>> graph = List<List<int>>.generate( |
| 168 | + numCourses, (_) => List<int>.filled(numCourses, 0)); |
| 169 | + |
| 170 | + // Create graph representation - adjacency matrix. |
| 171 | + for (int i = 0; i < prerequisites.length; i++) { |
| 172 | + graph[prerequisites[i][1]][prerequisites[i][0]] = 1; |
| 173 | + } |
| 174 | + |
| 175 | + return topoSort(graph, numCourses); |
| 176 | + } |
| 177 | + |
| 178 | + bool topoSort(List<List<int>> graph, int numCourses) { |
| 179 | + final List<bool> visited = List<bool>.filled(numCourses, false); |
| 180 | + final List<bool> tempMarked = List<bool>.filled(numCourses, false); |
| 181 | + final List<int> path = []; |
| 182 | + |
| 183 | + for (int i = 0; i < numCourses; i++) { |
| 184 | + if (!visited[i] && !dfsVisit(graph, i, path, visited, tempMarked)) { |
| 185 | + return false; |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + // TopoSort builds the actual schedule in the path. |
| 190 | + return path.length == numCourses; |
| 191 | + } |
| 192 | + |
| 193 | + bool dfsVisit(List<List<int>> graph, int course, List<int> path, |
| 194 | + List<bool> visited, List<bool> tempMarked) { |
| 195 | + if (tempMarked[course]) { |
| 196 | + return false; |
| 197 | + } |
| 198 | + if (!visited[course]) { |
| 199 | + tempMarked[course] = true; |
| 200 | + for (int i = 0; i < graph.length; i++) { |
| 201 | + if (graph[course][i] == 1) { |
| 202 | + if (!dfsVisit(graph, i, path, visited, tempMarked)) { |
| 203 | + return false; |
| 204 | + } |
| 205 | + } |
| 206 | + } |
| 207 | + tempMarked[course] = false; |
| 208 | + visited[course] = true; |
| 209 | + path.add(course); |
| 210 | + } |
| 211 | + return true; |
| 212 | + } |
| 213 | +} |
0 commit comments