📘
Course Schedule
MediumGraphs
Given numCourses and prerequisites [a, b] meaning b must be taken before a, return True if all courses can be finished (no cycles).
Input: first line = numCourses, second = prerequisites as a:b comma-separated (or 'none').
Example:
Input:
2 1:0
Output:
True
Reasoning:
- The input
2represents the total number of courses, and1:0represents a prerequisite where course1must be taken after course0. - We can model the courses as a graph, where each course is a node, and the prerequisites are directed edges, so
0 -> 1means course0is a prerequisite for course1. - Since there is only one edge and no cycles are present in the graph, all courses can be finished.
- The absence of cycles in the graph implies that a valid order of courses exists, so the function returns
True.
Constraints:
- 1 <= numCourses <= 2000
- 0 <= number of prerequisites <= 5000
Editor
Python 3.13.1
Test Results
0/0Run code to see test results.