5ms DFS beat 98% and 9ms BFS in java


  • 5
    L

    DFS:

    public class Solution {
        public boolean canFinish(int numCourses, int[][] prerequisites) {
            ArrayList<Integer>[] graph = new ArrayList[numCourses];//tricky
            for(int i = 0; i < numCourses; i++) {
                graph[i] = new ArrayList<Integer>();
            }
    
            for(int[] pre : prerequisites) {
                graph[pre[1]].add(pre[0]);
            }
    
            boolean[] visited = new boolean[numCourses],
            boolean[] finished = new boolean[numCourses];
            for(int i = 0; i < numCourses; i++) {
                if(!helper(graph, visited, finished, i)){
                    return false;
                }
            }
    
            return true;
        }
    
        public boolean helper(ArrayList[] graph, boolean[] visited, boolean[] finished, int s) {
            if(finished[s]) {
                return true;
            } else if(visited[s]) {
                return false;
            } else {
                visited[s] = true;
            }
    
            for(int i=0; i<graph[s].size();i++){
                int adj = (int)graph[s].get(i);//tricky
                if(!helper(graph, visited, finished, adj)){
                    return false;
                }
            }
    
            finished[s] = true;
            return true;
        }
    }
    

    BFS:

    public class Solution {
        public boolean canFinish(int numCourses, int[][] prerequisites) {
            List<List<Integer>> graph = new ArrayList<>();
            for(int i = 0; i < numCourses; i++) {
                graph.add(new ArrayList<>());
            }
    
            int[] indegree = new int[numCourses];
            for(int[] edge : prerequisites) {
                graph.get(edge[1]).add(edge[0]);
    
                indegree[edge[0]]++;
            }
    
            Queue<Integer> q = new LinkedList<>();
            for(int i = 0; i < numCourses; i++) {
                if(indegree[i] == 0){
                    q.offer(i);
                }
            }
    
            int count = 0;
            while(!q.isEmpty()) {
                int s = q.poll();
                for(int adj : graph.get(s)) {
                    indegree[adj]--;
                    count++;
                    if(indegree[adj] == 0) {
                        q.offer(adj);
                    }
                }
            }
    
            return count == prerequisites.length;
        }
    }
    

  • 0
    C

    Thanks for the solution. Similar idea. I just tried to make it more concise.
    Basically state is an int array that mimics visited (1) and finished (2).

    class Solution {
        public boolean canFinish(int num, int[][] preReqs) {
            List<Integer>[] adj = new List[num];
            for (int i = 0; i < adj.length; i++) adj[i] = new ArrayList<>();
            
            for (int[] edge : preReqs) {
                int from = edge[1], to = edge[0];
                adj[from].add(to);
            }
            
            int[] state = new int[num];
            for (int i = 0; i < num; i++) {
                if (!dfs(adj, state, i)) return false;
            }
            
            return true;
        }
        
        boolean dfs(List<Integer>[] adj, int[] state, int e) {
            if (state[e] == 2) return true;
            if (state[e] == 1) return false;
            state[e] = 1;
            
            for (int next : adj[e]) {
                if (!dfs(adj, state, next)) return false;
            }
            
            state[e] = 2;
            return true;
        }
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.