-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseSchedule.java
More file actions
37 lines (34 loc) · 1.03 KB
/
CourseSchedule.java
File metadata and controls
37 lines (34 loc) · 1.03 KB
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
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
HashMap<Integer,List<Integer>> map = new HashMap<>();
HashSet<Integer> visited = new HashSet<>();
for(int[] pre : prerequisites){
if(map.containsKey(pre[1]))
map.get(pre[1]).add(pre[0]);
else{
List<Integer> temp = new ArrayList<>();
temp.add(pre[0]);
map.put(pre[1], temp);
}
}
for(int i = 0;i < numCourses;i++){
if(cS(i, visited, map) == false)
return false;
}
return true;
}
private boolean cS(int i, HashSet<Integer> v, HashMap<Integer, List<Integer>> m){
if(v.contains(i))
return false;
if(m.get(i) == null)
return true;
v.add(i);
for(int pre : m.get(i)){
if(cS(pre,v,m) == false)
return false;
}
v.remove(i);
m.put(i, null);
return true;
}
}