-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCourseScheduleTwo.java
More file actions
39 lines (35 loc) · 1.04 KB
/
Copy pathCourseScheduleTwo.java
File metadata and controls
39 lines (35 loc) · 1.04 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
class Solution {
public boolean canFinish(int numCourses, int[][] prerequisites) {
HashMap<Integer, List<Integer>> adj = new HashMap<>();
for(int n = 0; n < numCourses; n++) {
adj.put(n, new ArrayList<Integer>());
}
for (int i = 0; i < prerequisites.length; i++) {
adj.get(prerequisites[i][1]).add(prerequisites[i][0]);
}
int[] incoming = new int[numCourses];
for (int n = 0; n < numCourses; n++) {
for (int i = 0; i < adj.get(n).size(); i++) {
incoming[adj.get(n).get(i)]++;
}
}
Deque<Integer> queue = new ArrayDeque<>();
for (int i = 0; i < incoming.length; i++) {
if (incoming[i] == 0) {
queue.add(i);
}
}
int count = 0;
while (!queue.isEmpty()) {
int node = queue.remove();
count++;
for (int i = 0; i < adj.get(node).size(); i++) {
incoming[adj.get(node).get(i)]--;
if (incoming[adj.get(node).get(i)] == 0) {
queue.add(adj.get(node).get(i));
}
}
}
return count == numCourses;
}
}