72. Course Schedule
Medium · Graph
You are given a list of courses, some of which have prerequisites. Each course is represented by a number, and prerequisites are given as pairs [course, prerequisite], meaning you must complete the prerequisite before taking the course.
Determine if it is possible to complete all courses. If there is a cycle in the prerequisite graph (a circular dependency), it is impossible to complete all courses.
Return true if all courses can be completed, false otherwise.
Examples
Example 1 Input: numCourses = 2, prerequisites = [[1, 0]] Output: true Explanation: There are 2 courses. To take course 1, you must first complete course 0. This is possible: take course 0, then course 1.
Example 2 Input: numCourses = 2, prerequisites = [[1, 0], [0, 1]] Output: false Explanation: There are 2 courses. Course 1 requires course 0, and course 0 requires course 1. This creates a cycle, so it is impossible to complete all courses.
Constraints
- Standard input/output constraints apply