#207
Course Schedule
medium· Graphsruns: 0There are numCourses courses labeled 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [a, b] indicates that you must take course b before course a. Return true if it is possible to finish all courses.
sign in to paste and practice your own solution
wpm 0acc 100%time 0:000 / 780
class Solution:
def canFinish(
self, numCourses: int, prerequisites: list[list[int]]
) -> bool:
graph = {i: [] for i in range(numCourses)}
for course, pre in prerequisites:
graph[course].append(pre)
visited = set()
def dfs(course: int) -> bool:
if course in visited:
return False
if not graph[course]:
return True
visited.add(course)
for pre in graph[course]:
if not dfs(pre):
return False
visited.remove(course)
graph[course] = []
return True
for course in range(numCourses):
if not dfs(course):
return False
return True
click the box to focus · tab inserts 4 spaces · backspace to correct · esc to pause
desktop only
codedrill is a typing game and needs a real keyboard. open this on a laptop or desktop to practice.
you can still browse problems and sections from your phone.