Last active
July 1, 2022 07:46
-
-
Save charlespunk/1b6e904ecf001122990f to your computer and use it in GitHub Desktop.
This file contains hidden or 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
public class Solution { | |
public boolean canFinish(int numCourses, int[][] prerequisites) { | |
Course[] courses = new Course[numCourses]; | |
for (int i = 0; i < courses.length; i ++) { | |
courses[i] = new Course(); | |
} | |
for (int[] edge : prerequisites) { | |
Course in = courses[edge[0]]; | |
Course out = courses[edge[1]]; | |
if (!out.outNeighbours.contains(in)) { | |
in.inDegree++; | |
out.outNeighbours.add(in); | |
} | |
} | |
Queue<Course> zeroInDegree = new LinkedList<Course>(); | |
for (Course course : courses) { | |
if (course.inDegree == 0) { | |
zeroInDegree.offer(course); | |
} | |
} | |
while (!zeroInDegree.isEmpty()) { | |
Course course = zeroInDegree.poll(); | |
for (Course outNeighbour : course.outNeighbours) { | |
outNeighbour.inDegree --; | |
if (outNeighbour.inDegree == 0) { | |
zeroInDegree.offer(outNeighbour); | |
} | |
} | |
course.outNeighbours.clear(); | |
} | |
for (Course course : courses) { | |
if (course.inDegree > 0) { | |
return false; | |
} | |
} | |
return true; | |
} | |
static class Course { | |
int inDegree; | |
Set<Course> outNeighbours; | |
Course() { | |
this.outNeighbours = new HashSet<>(); | |
} | |
} | |
} |
This file contains hidden or 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
public class Solution { | |
public int[] findOrder(int numCourses, int[][] prerequisites) { | |
Course[] courses = new Course[numCourses]; | |
for (int i = 0; i < courses.length; i ++) { | |
courses[i] = new Course(i); | |
} | |
for (int[] edge : prerequisites) { | |
Course in = courses[edge[0]]; | |
Course out = courses[edge[1]]; | |
if (!out.outNeighbours.contains(in)) { | |
in.inDegree++; | |
out.outNeighbours.add(in); | |
} | |
} | |
int[] order = new int[numCourses]; | |
int p = 0; | |
Queue<Course> zeroInDegree = new LinkedList<Course>(); | |
for (Course course : courses) { | |
if (course.inDegree == 0) { | |
zeroInDegree.offer(course); | |
order[p] = course.index; | |
p++; | |
} | |
} | |
while (!zeroInDegree.isEmpty()) { | |
Course course = zeroInDegree.poll(); | |
for (Course outNeighbour : course.outNeighbours) { | |
outNeighbour.inDegree --; | |
if (outNeighbour.inDegree == 0) { | |
zeroInDegree.offer(outNeighbour); | |
order[p] = outNeighbour.index; | |
p++; | |
} | |
} | |
course.outNeighbours.clear(); | |
} | |
for (Course course : courses) { | |
if (course.inDegree > 0) { | |
return new int[0]; | |
} | |
} | |
return order; | |
} | |
static class Course { | |
int index; | |
int inDegree; | |
Set<Course> outNeighbours; | |
Course(int index) { | |
this.index = index; | |
this.outNeighbours = new HashSet<>(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment