Tap here to switch tabs
Problem
Submissions

Problem: Course Schedule II

med
30 min
Understand how to solve course scheduling problems by applying topological sort to determine valid course completion orders. Learn to identify cycles that prevent finishing all courses and implement solutions efficiently.

Statement

You are given n courses, labeled from 0 to n - 1. Some courses have prerequisites, which are provided as a list of pairs: prerequisites[i] =[a,b]= [a, b]. To take course aa, you must first complete course bb.

Your task is to determine a valid order in which you can complete all the courses and return it as an array of course labels.

  • If there are multiple valid orderings, you can return any of them.

  • If it’s impossible to finish all courses (due to a cycle in prerequisites), return an empty array.

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
  • 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.
Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths
Tap here to switch tabs
Problem
Submissions

Problem: Course Schedule II

med
30 min
Understand how to solve course scheduling problems by applying topological sort to determine valid course completion orders. Learn to identify cycles that prevent finishing all courses and implement solutions efficiently.

Statement

You are given n courses, labeled from 0 to n - 1. Some courses have prerequisites, which are provided as a list of pairs: prerequisites[i] =[a,b]= [a, b]. To take course aa, you must first complete course bb.

Your task is to determine a valid order in which you can complete all the courses and return it as an array of course labels.

  • If there are multiple valid orderings, you can return any of them.

  • If it’s impossible to finish all courses (due to a cycle in prerequisites), return an empty array.

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
  • 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.
Unlock AI-Powered LearningUpgrade to smarter learning with instant explanations of Ask Agent, Personalized Interview Prep, Real-World Projects, 3 AI Mock Interviews per month, and Personalized Paths