Course Schedule

Medium

Course Schedule

Problem Statement

Given a list of courses with their corresponding prerequisites, determine whether it is possible to finish all courses successfully. A student can only take at most one course at a time, and must complete the prerequisites for a course before taking it.

Input Description

  • numCourses
    : The total number of courses.
  • prerequisites
    : A 2D array representing the prerequisites for each course. The
    [i][j]
    -th entry denotes that course
    i
    is a prerequisite for course
    j
    .

Output Description

  • A boolean value indicating whether it is possible to finish all courses successfully.

Rules and Constraints

  • You should not use any built-in graph library functions.
  • The graph should be directed, and its vertices and edges will not contain self-loops. However, there may be multiple edges between two vertices (i.e., multiple prerequisites for a course).
  • You can have at most
    numCourses
    vertices in the graph, and the number of edges will not exceed
    numCourses * (numCourses - 1)
    .
  • The input graph is guaranteed to be strongly connected if the result is possible, implying that there is a valid ordering if the graph has a sink vertex.

Example

Input: {"input_data":[1,2,3]} Output: [1,2,3]

CompaniesGoogleMetaAmazon
JavaScript

Login to write code

Solve problems, verify your skills, and earn XP.