-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
83 lines (69 loc) · 2.28 KB
/
Solution.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package org.example.problems.course_schedule;
import org.example.problems.SolutionInterface;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;
public class Solution implements SolutionInterface {
@Override
public String getName() {
return "Course Schedule";
}
@Override
public String getUrl() {
return "https://leetcode.com/problems/course-schedule/";
}
private static class Solver {
ArrayList<HashSet<Integer>> dependencies;
int[][] prerequisites;
int numCourses;
private final HashSet<Integer> checked = new HashSet<>();
Solver(int numCourses, int[][] prerequisites) {
dependencies = new ArrayList<>(numCourses);
for (int i = 0; i < numCourses; i++) {
dependencies.add(new HashSet<>());
}
this.prerequisites = prerequisites;
this.numCourses = numCourses;
}
public boolean hasCycle() {
for (int[] dep : prerequisites) {
if (dep[0] == dep[1]) {
return true;
}
dependencies.get(dep[0]).add(dep[1]);
}
for (int i = 0; i < numCourses; i++) {
if (checked.contains(i)) {
continue;
}
for (int d : dependencies.get(i)) {
if (isCycled(i, d, new HashSet<>())) {
return true;
}
}
}
return false;
}
private boolean isCycled(int root, int node, Set<Integer> diveCheck) {
if (root == node || diveCheck.contains(node)) {
return true;
}
if (checked.contains(node)) {
return false;
}
checked.add(node);
diveCheck.add(node);
for (int dep : dependencies.get(node)) {
if (isCycled(root, dep, diveCheck)) {
return true;
}
}
diveCheck.remove(node);
return false;
}
}
public boolean canFinish(int numCourses, int[][] prerequisites) {
Solver s = new Solver(numCourses, prerequisites);
return !s.hasCycle();
}
}