Home
Log in
DescriptionSubmissionsSolution
DescriptionSubmissionsSolution
Loading...

Log in to run or submit

numCourses =
prerequisites =

Codey

Practise coding problems, test your solutions and track your progress.

Explore

  • Problems
  • About
  • Contact

Legal

  • Privacy Policy
  • Terms of Use

© 2026 Codey. Personal learning project.

Solution
Updated: 2026-02-23

Idea

You can finish all courses iff the prerequisite graph has no cycle.

Approach

Topological sort (Kahn’s algorithm):

  • Build adjacency b -> a for prerequisite b before a.
  • Track indegrees.
  • Repeatedly take indegree-0 nodes.

Why it works

A directed graph is acyclic iff a topological ordering visits all nodes.

Code
Loading...
Complexity
Time: O(V+E)
Space: O(V+E)
Solution
Updated: 2026-02-23

Idea

You can finish all courses iff the prerequisite graph has no cycle.

Approach

Topological sort (Kahn’s algorithm):

  • Build adjacency b -> a for prerequisite b before a.
  • Track indegrees.
  • Repeatedly take indegree-0 nodes.

Why it works

A directed graph is acyclic iff a topological ordering visits all nodes.

Code
Loading...
Complexity
Time: O(V+E)
Space: O(V+E)