PIXELBANKv9.1.0
Menu

Course Schedule II

There are numCourses courses labeled from 0 to numCourses - 1. You are given prerequisites where prerequisites[i] = [a, b] means you must take course b before course a.

Return an ordering of courses you should take to finish all courses. If impossible, return an empty list.

Output the order as space-separated integers. If multiple valid orderings exist, return any one.

Example:

Input:
4
1,0;2,0;3,1;3,2
Output:
0 1 2 3
Reasoning:
  • The input 4 represents the total number of courses, labeled from 0 to 3.
  • The prerequisites 1,0;2,0;3,1;3,2 indicate that course 0 is a prerequisite for courses 1 and 2, and courses 1 and 2 are prerequisites for course 3.
  • We can start by taking course 0, as it has no prerequisites, then take courses 1 and 2, which both require course 0, and finally take course 3, which requires both courses 1 and 2.
  • The resulting ordering is 0 1 2 3, which satisfies all the given prerequisites.

Constraints:

  • 1 <= numCourses <= 2000
  • 0 <= len(prerequisites) <= numCourses * (numCourses - 1)
  • All pairs are unique
🔒

Editor locked

The code editor is locked for Pro problems. It is only available for free problems. Please upgrade to gain access to the code editor for all problems.

solution.py

Test Results

0/0
Run code to see test results.
Course Schedule II - Medium | PixelBank