-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathCourseSchedule4.java
30 lines (26 loc) · 1.1 KB
/
CourseSchedule4.java
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
/*https://leetcode.com/problems/course-schedule-iv/*/
class Solution {
public List<Boolean> checkIfPrerequisite(int numCourses, int[][] prerequisites, int[][] queries) {
ArrayList<ArrayList<Integer>> graph = new ArrayList<ArrayList<Integer>>();
int i, j, k;
for (i = 0; i < numCourses; ++i) graph.add(new ArrayList<Integer>());
//create a graph for the given prerequisites
boolean[][] reachable = new boolean[numCourses][numCourses];
for (i = 0; i < prerequisites.length; ++i)
reachable[prerequisites[i][0]][prerequisites[i][1]] = true;
for (i = 0; i < numCourses; ++i)
{
for (j = 0; j < numCourses; ++j)
{
for (k = 0; k < numCourses; ++k)
{
if (!reachable[j][k] && reachable[j][i] && reachable[i][k])
reachable[j][k] = true;
}
}
}
List<Boolean> result = new ArrayList<Boolean>();
for (int[] q : queries) result.add(reachable[q[0]][q[1]]);
return result;
}
}