...

/

Solution: Course Schedule II

Solution: Course Schedule II

Let's solve the Course Schedule II problem using the Topological Sort pattern.

Statement

Let’s assume that there are a total of nn courses labeled from 00 to n1n-1. Some courses may have prerequisites. A list of prerequisites is specified such that if Prerequisitesi=a,bPrerequisites_i = {a, b}, you must take course bb before course aa.

Given the total number of courses nn and a list of the prerequisite pairs, return the course order a student should take to finish all of the courses. If there are multiple valid orderings of courses, then the return any one of them.

Note: There can be a course in the 00 to n1n-1 range with no prerequisites.

Constraints:

Let nn be the number of courses.

  • 1n15001 \leq n \leq 1500
...