bugfree Icon
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course
interview-course

Leetcode Problem 210. Course Schedule II

210. Course Schedule II

Leetcode Solutions

Approach: Using Depth First Search

  1. Create a stack S to store the topologically sorted courses.
  2. Construct an adjacency list from the prerequisites list.
  3. Perform a DFS for each course that has not been visited.
  4. During DFS, for each course, visit all its unvisited prerequisites.
  5. After visiting all prerequisites for a course, add the course to the stack.
  6. After all courses are processed, pop the stack to get the courses in the correct order.
  7. Return the stack as an array if all courses are visited; otherwise, return an empty array.
UML Thumbnail

Approach: Using Node Indegree

Ask Question

Programming Language
image/screenshot of info(optional)
Full Screen
Loading...

Suggested Answer

Answer
Full Screen
Copy Answer Code
Loading...