Course Schedule II

Try to solve the Course Schedule II problem.


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.


Let nn be the number of courses.

  • 1n15001 \leq n \leq 1500
  • 00 \leq prerequisites.length 1000\leq 1000
  • prerequisites[i].length ==2== 2
  • 0a, b<n0 \leq a, \space b < n
  • aba \neq b
  • All the pairs [a, b][a, \space b] are distinct.


Create a free account to view this lesson.

By signing up, you agree to Educative's Terms of Service and Privacy Policy