Skip to content

Commit fbcdfc7

Browse files
committed
leetcode
1 parent f11390e commit fbcdfc7

File tree

4 files changed

+305
-0
lines changed

4 files changed

+305
-0
lines changed

CourseSchedule/course_schedule.dart

+213
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
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+
}

CourseSchedule/course_schedule.go

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package main
2+
3+
4+
5+
6+
func canFinish(numCourses int, prerequisites [][]int) bool {
7+
graph := make([][]int, numCourses)
8+
for i := 0; i < numCourses; i++ {
9+
graph[i] = make([]int, 0)
10+
}
11+
12+
for _, pre := range prerequisites {
13+
x := pre[0]
14+
y := pre[1]
15+
graph[x] = append(graph[x], y)
16+
}
17+
18+
visited := make([]int, numCourses)
19+
for i := 0; i < numCourses; i++ {
20+
if hasCycle(graph, visited, i) {
21+
return false // Cycle detected
22+
}
23+
}
24+
25+
return true
26+
}
27+
28+
func hasCycle(graph [][]int, visited []int, node int) bool {
29+
if visited[node] == 1 {
30+
return true // Cycle detected
31+
}
32+
if visited[node] == -1 {
33+
return false // Already visited and no cycle
34+
}
35+
36+
visited[node] = 1 // Mark node as visited
37+
38+
for _, neighbor := range graph[node] {
39+
if hasCycle(graph, visited, neighbor) {
40+
return true // Cycle detected
41+
}
42+
}
43+
44+
visited[node] = -1 // Mark node as visited and no cycle
45+
return false
46+
}
47+
48+
49+
50+

CourseSchedule/course_schedule.md

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#
2+
3+
## Topological Sort
4+
5+
```dart
6+
import 'dart:collection';
7+
8+
class Solution {
9+
bool canFinish(int numCourses, List<List<int>> prerequisites) {
10+
final List<int> answer = [];
11+
final Map<int, List<int>> map = Map();
12+
final List<int> result = List<int>.filled(numCourses, 0);
13+
14+
for (final List<int> x in prerequisites) {
15+
map[x[1]] = [...map[x[1]] ?? [], x[0]];
16+
result[x[0]]++;
17+
}
18+
19+
final Queue<int> q = Queue<int>();
20+
for (int i = 0; i < numCourses; i++) {
21+
if (result[i] == 0) {
22+
q.add(i);
23+
}
24+
}
25+
26+
while (q.isNotEmpty) {
27+
final int fr = q.removeFirst();
28+
answer.add(fr);
29+
30+
for (final int x in map[fr] ?? []) {
31+
result[x]--;
32+
if (result[x] == 0) {
33+
q.add(x);
34+
}
35+
}
36+
}
37+
38+
return answer.length == numCourses;
39+
}
40+
}
41+
```

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ This repo contain leetcode solution using DART and GO programming language. Most
246246
- [**2024.** Maximize the Confusion of an Exam](MaximizeTheConfusionOfAnExam/maximize_the_confusion_of_an_exam.dart)
247247
- [**2551.** Put Marbles in Bags](PutMarblesInBags)
248248
- [**863.** All Nodes Distance K in Binary Tree](AllNodesDistanceKInBinaryTree)
249+
- [**207.** Course Schedule](CourseSchedule)
249250

250251
## Reach me via
251252

0 commit comments

Comments
 (0)