Tap here to switch tabs
Problem
Submissions

Problem: Parallel Courses

med
30 min
Explore how to apply topological sort to schedule parallel courses based on prerequisite dependencies. Learn to identify the minimum number of semesters required to complete all courses and detect impossible schedules caused by circular dependencies.

Statement

You are designing a course schedule for a university with n courses, labeled from 1 to n. The prerequisite requirements are given in an array, relations, where each relations[i]=[prevCoursei,nextCoursei]\text{relations}[i] = [\text{prevCourse}_i, \text{nextCourse}_i] means that prevCoursei\text{prevCourse}_i must be completed before you can enroll in nextCoursei\text{nextCourse}_i.

You can take any number of courses during a single semester, as long as the prerequisite for each course has been met in a previous semester.

Your task is to determine the minimum number of semesters required to complete all n courses. If the prerequisites contain a circular dependency (e.g., course A requires B, and course B requires A), it’s impossible to complete them all, so you should return -1.

Constraints:

  • 11 \leq n 5×103\leq 5 \times10^3

  • 11 \leq relations.length 5×103\leq 5 \times 10^3

  • relations[i].length ==2== 2

  • 1prevCoursei1 \leq \text{prevCourse}_i, nextCoursei\text{nextCourse}_i \leq n

  • prevCourseinextCoursei\text{prevCourse}_i \neq \text{nextCourse}_i

  • All the pairs [prevCoursei,nextCoursei][\text{prevCourse}_i, \text{nextCourse}_i] are unique.

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: Parallel Courses

med
30 min
Explore how to apply topological sort to schedule parallel courses based on prerequisite dependencies. Learn to identify the minimum number of semesters required to complete all courses and detect impossible schedules caused by circular dependencies.

Statement

You are designing a course schedule for a university with n courses, labeled from 1 to n. The prerequisite requirements are given in an array, relations, where each relations[i]=[prevCoursei,nextCoursei]\text{relations}[i] = [\text{prevCourse}_i, \text{nextCourse}_i] means that prevCoursei\text{prevCourse}_i must be completed before you can enroll in nextCoursei\text{nextCourse}_i.

You can take any number of courses during a single semester, as long as the prerequisite for each course has been met in a previous semester.

Your task is to determine the minimum number of semesters required to complete all n courses. If the prerequisites contain a circular dependency (e.g., course A requires B, and course B requires A), it’s impossible to complete them all, so you should return -1.

Constraints:

  • 11 \leq n 5×103\leq 5 \times10^3

  • 11 \leq relations.length 5×103\leq 5 \times 10^3

  • relations[i].length ==2== 2

  • 1prevCoursei1 \leq \text{prevCourse}_i, nextCoursei\text{nextCourse}_i \leq n

  • prevCourseinextCoursei\text{prevCourse}_i \neq \text{nextCourse}_i

  • All the pairs [prevCoursei,nextCoursei][\text{prevCourse}_i, \text{nextCourse}_i] are unique.

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