PIXELBANKv8.2.1
Menu

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 2 represents the total number of courses, and 1:0 represents a prerequisite where course 1 must be taken after course 0.
  • We can model the courses as a graph, where each course is a node, and the prerequisites are directed edges, so 0 -> 1 means course 0 is a prerequisite for course 1.
  • 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

Test Results

0/0
Run code to see test results.