-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
class Solution { | ||
HashMap<Integer, GraphNode> graph = new HashMap<>(); | ||
|
||
public boolean canFinish(int n, int[][] preq) { | ||
if (preq.length == 0) return true; | ||
int totalCourses = preq.length; | ||
for (int[] edge : preq) { | ||
GraphNode prev = getOrCreate(edge[0]); | ||
GraphNode next = getOrCreate(edge[1]); | ||
prev.nodes.add(edge[1]); | ||
next.inCount += 1; | ||
} | ||
ArrayList<Integer> noPreqCourses = new ArrayList<>(); | ||
for (Map.Entry<Integer, GraphNode> entry : graph.entrySet()) { | ||
GraphNode node = entry.getValue(); | ||
if (node.inCount == 0) | ||
noPreqCourses.add(entry.getKey()); | ||
} | ||
|
||
int coursesDone = 0; | ||
while (noPreqCourses.size() > 0) { | ||
int course = noPreqCourses.get(noPreqCourses.size() - 1); | ||
noPreqCourses.remove(noPreqCourses.size() - 1); | ||
for (int nextCourse : graph.get(course).nodes) { | ||
GraphNode child = graph.get(nextCourse); | ||
child.inCount -= 1; | ||
coursesDone++; | ||
if (child.inCount == 0) | ||
noPreqCourses.add(nextCourse); | ||
} | ||
} | ||
if (totalCourses == coursesDone) return true; | ||
return false; | ||
|
||
} | ||
|
||
public GraphNode getOrCreate(int node) { | ||
if (!graph.containsKey(node)) { | ||
graph.put(node, new GraphNode()); | ||
} | ||
return graph.get(node); | ||
} | ||
} | ||
|
||
class GraphNode { | ||
int inCount; | ||
ArrayList<Integer> nodes = new ArrayList<Integer>(); | ||
} |