-
Notifications
You must be signed in to change notification settings - Fork 2.3k
/
0210-course-schedule-ii.cs
53 lines (49 loc) · 1.71 KB
/
0210-course-schedule-ii.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class Solution {
List<int> output = null;
public int[] FindOrder(int numCourses, int[][] prerequisites)
{
IDictionary<int, List<int>> preMap = new Dictionary<int, List<int>>();
HashSet<int> visited = new HashSet<int>();
HashSet<int> cycle = new HashSet<int>();
output = new List<int>();
for (int i = 0; i < numCourses; i++)
{
preMap.Add(i, new List<int>());
}
foreach (int[] course in prerequisites)
{
int courseToTake = course[0];
int courseDependOn = course[1];
preMap[courseToTake].Add(courseDependOn);
}
foreach (int c in Enumerable.Range(0, numCourses))
{
if (DfsGraphTopologicalSort(preMap, visited, cycle, c) == false)
{
return Array.Empty<int>();
}
}
return output.ToArray();
}
public bool DfsGraphTopologicalSort(IDictionary<int, List<int>> preMap, HashSet<int> visited, HashSet<int> cycle, int crs)
{
if (cycle.Contains(crs)) return false;
if (visited.Contains(crs)) return true;
if (preMap[crs] == new List<int>())
{
return true;
}
cycle.Add(crs);
foreach (var pre in preMap[crs])
{
if (DfsGraphTopologicalSort(preMap, visited,cycle, pre)==false)
{
return false;
}
}
cycle.Remove(crs);
visited.Add(crs);
output.Add(crs);
return true;
}
}