Title Description:
There is a total of n courses you have to take, labeled from 0 to N-1.
Some courses May has prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a Pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, return the ordering of courses you should take to fini SH all courses.
There may is multiple correct orders, you just need to return one of the them. If It is impossible-to-finish all courses, return an empty array.
For example:
2, [[1,0]]
There is a total of 2 courses to take. To take course 1 should has finished course 0. So the correct course order is [0,1]
4, [[1,0],[2,0],[3,1],[3,2]]
There is a total of 4 courses to take. To take course 3 should has finished both courses 1 and 2. Both Courses 1 and 2 should is taken after you finished course 0. So one correct course order is [0,1,2,3]. Another correct ordering is[0,2,1,3].
Input, need to select the number of courses N, and course dependency pres, output an array of elective courses. The expression for the dependency of course a depends on course b is [A, b].
Ideas:
Create a list of dependencies to hold all classes that depend on other lessons to be selected, indicating the number of dependent courses. i.e. initialize Dependency[n], traverse PRES[0...N] [dependency[pres[i,1]] + = 1;
Find the Course collection every time: {No classes are dependent on any course}. Add it to the course selection results and update the dependency list Dependency[n] until {No classes are dependent on the class} is empty. is a process of BFS.
Find {Classes not dependent on any course}: Traverse dependency table: Pres[i], where i∈[0,n-1], judging dependency[pres[i,0]] is greater than 0.
Implementation code:
public class Solution {public int[] findorder (int numcourses, int[,] prerequisites) {var row = prerequisites. GetLength (0); if (row = = 0) {int[] c = new int[numcourses]; for (int i=0; i<numcourses; i++) {c[i]=i; } return C; }//Save [CourseID, Refcount]var refcounttable = new Int[numcourses];for (var i = 0;i < row; i++) {Refcounttable[prerequi SITES[I,0]] + +;} No ref coursesvar norefcourses = new Dictionary<int, int> (); for (var i = 0;i < row; i++) {//Find courses does n OT appear in ref tableif (refcounttable[prerequisites[i,1] = = 0 &&!norefcourses.containskey (prerequisites[i,1 ]) {norefcourses.add (prerequisites[i,1],1);}} var size = Norefcourses.count;var result = new Int[numcourses];var Count = 0;while (Norefcourses.count > 0) {var c = noRe FCourses.Keys.First (); result[count++] = C;norefcourses.remove (c); for (var i = 0;i < row; i++) {if (prerequisites[i,1] = = c) {refcounttable[prerequisites[i,0]]--;if (refcounttable[prerequisites[i,0]] = = 0 &&!norefcourses.containskey (prerequisites[i,1])) {Norefcourses.add (prerequisites[i,0], 1); size++;}}}} if (size = = numcourses) {return result;} return new int[0];}}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Leetcode--Course Schedule II