Skip to content

Bug Report for course-schedule #4716

@vatsuak

Description

@vatsuak

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

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy