text function canFinish(numCourses, prerequisites): graph = build adjacency list state = array of UNVISITED function hasCycle(course): if state[course] == VISITING: return true // cycle detected if state[course] == VISITED: return false // already explored state[course] = VISITING for prereq in graph[course]: if hasCycle(prereq): return true state[course] = VISITED return false for course from 0 to numCourses-1: if hasCycle(course): return false return true Time complexity: where = courses, = prerequisites. Space complexity: for the graph and recursion stack.
##### ###### ##### ### # # ### # # ###### ## ## ## ## ## ## ## # # # # # ## ##### #### ##### # # # # # # # #### ## # ## ## ## ## # # # # # ## ## # ###### ## ### # ### # ######
$ curl repovive.com/roadmaps/maang-interview-prep/graphs/course-schedule-pseudocode
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░░███████████████████████████████████████████████████████████████████████████████████