Course Schedule II Problem

Course Schedule II Problem — ExecCode Medium DSA Practice

Solve the Course Schedule II problem on ExecCode. Free online medium DSA practice in Graph. Write and run code in Java, C++, Python — no signup required to run.

Problem description

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai. Return the ordering of courses you should take to finish all courses. If there are many valid orderings, return any of them. If it is impossible to finish all courses, return an empty array.

Examples

Input numCourses = 6, prerequisites = [[1, 0], [2, 0], [3, 1], [3, 2], [4, 3], [5, 4]]; Output [0, 1, 2, 3, 4, 5]. Input numCourses = 2, prerequisites = [[1, 0]]; Output [0, 1]. Input numCourses = 2, prerequisites = [[1, 0], [0, 1]]; Output []

Constraints

1 ≤ numCourses ≤ 2000 0 ≤ prerequisites.length ≤ numCourses * (numCourses - 1) prerequisites[i].length == 2 0 ≤ ai, bi < numCourses ai != bi All the pairs [ai, bi] are unique.

Practice Course Schedule II free on ExecCode. Browse DSA problems, topic map, and placement guides.