-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathlc207.java
More file actions
49 lines (48 loc) · 1.81 KB
/
Copy pathlc207.java
File metadata and controls
49 lines (48 loc) · 1.81 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
38
39
40
41
42
43
44
45
46
47
48
49
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
int n = numCourses;
int m = prerequisites.length;
HashSet<Integer> courses = new HashSet<Integer>(); ///courses with out pre-reqs
Map<Integer,ArrayList<Integer>> graph = new HashMap<Integer,ArrayList<Integer>>();
Map<Integer,ArrayList<Integer>> incoming = new HashMap<Integer,ArrayList<Integer>>();
for(int i=0;i<n;i++)
{
courses.add(i);
graph.put(i,new ArrayList<Integer>());
incoming.put(i, new ArrayList<Integer>());
}
for(int i=0;i<m;i++)
{
int course = prerequisites[i][0];
int prereq = prerequisites[i][1];
//step-1
courses.remove(prereq);
graph.get(course).add(prereq);
incoming.get(prereq).add(course);
}
//now courses hashset will have only the courses which dont have pre-req courses.
if(courses.size()==0)
return false;
List<Integer> orderOfCourses = new ArrayList<Integer>();
//step2
while(courses.size() >0)
{
//Console.WriteLine("course={0}",string.Join(" ",courses));
int cur = courses.iterator().next();
courses.remove(cur);
orderOfCourses.add(cur);
Integer[] edges = graph.get(cur).toArray(new Integer[0]);
for(int e : edges)
{
//remove those edges from graph.
graph.get(cur).remove(new Integer(e));
incoming.get(e).remove(new Integer(cur));
if(incoming.get(e).size()==0)
{
courses.add(e);
}
}
}
return orderOfCourses.size() == n;
}
}