-
Notifications
You must be signed in to change notification settings - Fork 1
/
210. Course Schedule II
43 lines (42 loc) · 1.15 KB
/
210. Course Schedule II
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
class Solution {
public int[] findOrder(int numCourses, int[][] prerequisites) {
int a[] = new int[numCourses],c=0;
int y[] = new int[numCourses];
int z[] = new int[0];
int n=prerequisites.length,i;
HashSet<Integer> l =new HashSet<Integer>();
for(i=0;i<n;i++)
a[prerequisites[i][0]]++;
for(i=0;i<numCourses;i++)
if(a[i]==0)
{
l.add(i);
y[c++]=i;
}
int f=1,x=0;
while(f==1)
{
f=0;
x=0;
for(i=0;i<n;i++)
{
if(prerequisites[i][0] == -1)
continue;
x=1;
if(l.contains(prerequisites[i][1]))
{
if(--a[prerequisites[i][0]] == 0)
{
if(l.add(prerequisites[i][0]))
y[c++]=prerequisites[i][0];
}
f=1;
prerequisites[i][0] = -1;
}
}
}
if(x==1)
return z;
return y;
}
}