Skip to content

Bug Report for course-schedule #4403

@ikurooo

Description

@ikurooo

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.

Hi,
I was solving the problem and noticed something suspicious,

`class Solution {
Set visited = new HashSet<>();
Map<Integer, List> graph;

public boolean canFinish(int numCourses, int[][] prerequisites) {
    this.graph = new HashMap<>();
    for (int i = 0; i < numCourses; i++) {
        graph.put(i, new ArrayList<>());
    }

    for (int[] prerequisite : prerequisites) {
        graph.get(prerequisite[0]).add(prerequisite[1]);
    }
    
    for (int i = 0; i < numCourses; i++) {
        if (!dfs(i)) {
            return false;
        }
    }

    return true;
}

public boolean dfs(int course) {
    if (visited.contains(course))
        return false;
    
    for (int i = 0; i < graph.get(course).size(); i++) {
        visited.add(course);
        boolean res = dfs(graph.get(course).get(i));
        visited.remove(course);
        return res;
    }
    
    return true;
}

}`
this ran through without issue even though this only checks one path, adding the following test case would exclude the above code as a valid answer:

[[2,0],[1,0],[3,1],[3,2],[1,3]]

Best,
Ivan

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