Untitled

 avatar
unknown
plain_text
2 months ago
1.1 kB
3
Indexable
Imagine you are enrolling in a series of training courses. Each course may have prerequisites, meaning you must complete certain courses before taking others. You are given numCourses, representing the total number of courses, labeled from 0 to numCourses - 1. Additionally, you are given a list of prerequisite pairs, where each pair [A, B] indicates that you must complete course B before taking course A.

Your task is to determine whether it is possible to complete all the courses. If there is a cycle—where courses depend on each other in a way that makes completion impossible—return false. Otherwise, return true.

Example 1:
Input:
numCourses = 2, prerequisites = [[1,0]]
Output:
true
Explanation:
There are two courses in total. To take course 1, you must first complete course 0, which is possible.

Example 2:
Input:
numCourses = 2, prerequisites = [[1,0],[0,1]]
Output:
false
Explanation:
Course 1 requires 0, but course 0 also requires 1, forming a cycle. This makes it impossible to complete both courses.

Your goal is to check if all courses can be completed without any cyclic dependencies.
 
Editor is loading...
Leave a Comment