-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Closed
Description
Bug Report for https://neetcode.io/problems/course-schedule
Please describe the bug below and include any steps to reproduce the bug or screenshots if possible.
Please add a test case like so:
numCourses=5
prerequisites=[[0,1],[0,2],[2,3],[1,3],[3,4]]
My code passed all the test cases, but it would fail in the above case which was not part of the tests.
Code for reproducing the error:
class Solution:
def canFinish(self, numCourses: int, prerequisites: List[List[int]]) -> bool:
adj_dict = {i: [] for i in range(numCourses)}
for prereq_a, prereq_b in prerequisites:
adj_dict[prereq_a].append(prereq_b)
for i in range(numCourses):
stack = [i]
seen = set()
while len(stack):
node_to_visit = stack.pop()
if (node_to_visit in seen) and (adj_dict[node_to_visit] != []):
return False
else:
seen.add(node_to_visit)
for prereq in adj_dict[node_to_visit]:
stack.append(prereq)
return True
Metadata
Metadata
Assignees
Labels
No labels