forked from neetcode-gh/leetcode
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path0207-course-schedule.cs
50 lines (47 loc) · 1.46 KB
/
0207-course-schedule.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
publicclassSolution{
publicboolCanFinish(intnumCourses,int[][]prerequisites)
{
IDictionary<int,List<int>>preMap=newDictionary<int,List<int>>();
HashSet<int>visited=newHashSet<int>();
for(inti=0;i<numCourses;i++)
{
preMap.Add(i,newList<int>());
}
foreach(int[]courseinprerequisites)
{
intcourseToTake=course[0];
intcourseDependOn=course[1];
preMap[courseToTake].Add(courseDependOn);
}
foreach(intcinEnumerable.Range(0,numCourses))
{
if(!DfsGraph(preMap,visited,c))
{
returnfalse;
}
}
returntrue;
}
publicboolDfsGraph(IDictionary<int,List<int>>preMap,HashSet<int>visited,intcrs)
{
if(visited.Contains(crs))
{
returnfalse;
}
if(preMap[crs]==newList<int>())
{
returntrue;
}
visited.Add(crs);
foreach(varpreinpreMap[crs])
{
if(!DfsGraph(preMap,visited,pre))
{
returnfalse;
}
}
visited.Remove(crs);
preMap[crs]=newList<int>();
returntrue;
}
}