class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
List<Integer>[] arr = new ArrayList[numCourses];
int[] inDegree = new int[numCourses];
for(int i=0; i<numCourses; i++) {
arr[i] = new ArrayList<Integer>();
}
for(int i=0; i<prerequisites.length; i++) {
arr[prerequisites[i][0]].add(prerequisites[i][1]);
inDegree[prerequisites[i][1]]++;
}
Stack<Integer> stack = new Stack<>();
for(int i=0; i<numCourses; i++) {
if(inDegree[i] == 0) {
stack.push(i);
}
}
int cnt = 0;
while(!stack.isEmpty()) {
int del = stack.pop();
cnt++;
for(int i=0; i<arr[del].size(); i++) {
int tmp = arr[del].get(i);
inDegree[tmp]--;
if(inDegree[tmp] == 0) {
stack.push(tmp);
}
}
}
return cnt == numCourses;
}
}