Skip to content

210. Course Schedule II

There are a total of numCourses courses you have to take, labeled from 0 to numCourses - 1. You are given an array prerequisites where prerequisites[i] = [ai, bi] indicates that you must take course bi first if you want to take course ai.

  • For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.

Return the ordering of courses you should take to finish all courses. If there are many valid answers, return any of them. If it is impossible to finish all courses, return an empty array.

Example 1:

Input: numCourses = 2, prerequisites = [[1,0]]
Output: [0,1]
Explanation: There are a total of 2 courses to take. To take course 1 you should have finished course 0. So the correct course order is [0,1].

Example 2:

Input: numCourses = 4, prerequisites = [[1,0],[2,0],[3,1],[3,2]]
Output: [0,2,1,3]
Explanation: There are a total of 4 courses to take. To take course 3 you should have finished both courses 1 and 2. Both courses 1 and 2 should be taken after you finished course 0.
So one correct course order is [0,1,2,3]. Another correct ordering is [0,2,1,3].

Example 3:

Input: numCourses = 1, prerequisites = []
Output: [0]

Solution:

class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
       int[] ans = new int[numCourses]; // [0, 1, 2, 3]

       Map<Integer, List<Integer>> preMap = new HashMap<>();
       // 0, []
       // 1, [0]
       // 2, [0]
       // 3, [1,2]

       for (int i = 0; i < numCourses; i++){
        preMap.put(i, new ArrayList<Integer>());
       }

       for (int[] row : prerequisites){// [1,0]//[2,0] //[3,1]// [3,2]
        int course = row[0]; // 1 // 2  // 3
        int prereq = row[1];// 0 //  0 // 1

        // List<Integer> allPrereq = preMap.get(course); // 1,[]
        // allPrereq.add(prereq);// 1, [0]
        // preMap.put(course, allPrereq); //  1,[0]



        preMap.get(course).add(prereq); // 

       }
       // 0, []
       // 1, [0]
       // 2, [0]
       // 3, [1,2]

       Set<Integer> visited = new HashSet<>();
       // Set to keep track of visited nodes
       Set<Integer> cycle = new HashSet<>();
       // Set to detect cycles in the graph.

       List<Integer> ansList = new ArrayList<>();
       // List to store the courses in the order of completion.

       for (int i = 0; i < numCourses; i++){
        if (!dfs(i, preMap, ansList, visited, cycle)){
            return new int[]{};
        }
       }

       for (int i = 0; i < ansList.size(); i++){
        ans[i] = ansList.get(i);
       }

       return ans;
    }

    public boolean dfs(int course, Map<Integer, List<Integer>> preMap, List<Integer> ansList, Set<Integer> visited, Set<Integer> cycle){
        if (cycle.contains(course)){
            return false;
        }

        // If the course is in the cycle set, we found a cycle, return false.

        if (visited.contains(course)){
            return true;
        }
        // If the course is already visited, no need to process it again.

        cycle.add(course);

        for(int prereq : preMap.get(course)){
            if (!dfs(prereq, preMap, ansList, visited, cycle)){
                return false;
            }
        }
        // Recursively perform DFS on all prerequisites of the course.

        cycle.remove(course);
        // Remove the course from the cycle set as we're done processing it.
        visited.add(course);
        // Mark the course as visited.
        ansList.add(course);
        // Return true as the course can be completed.
        return true;
    }
}
// TC: O(V+E)
// SC: O(V+E)
class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        // the adjacency list representation of prerequisites.
        List<List<Integer>> graph = new ArrayList<>(numCourses);
        for (int i = 0; i < numCourses; i++){
            graph.add(new ArrayList<>());
        }

        for (int[] dependency : prerequisites){
            int x = dependency[0];
            int y = dependency[1];
            graph.get(y).add(x);
        }

        return topologicalSort(graph);
    }

    private int[] topologicalSort(List<List<Integer>> graph){
        int numCourses = graph.size();
        int[] topologicalOrder = new int[numCourses];
        int[] incomingEdges = new int[numCourses];
        for (int x = 0; x < numCourses; x++){
            for (int y : graph.get(x)){
                incomingEdges[y]++;
            }
        }

        Queue<Integer> queue = new ArrayDeque<>();
        for (int x = 0; x < numCourses; x++){
            if (incomingEdges[x] == 0){
                queue.offer(x);
            }
        }

        int numExpanded = 0;
        while (!queue.isEmpty()){
            int x = queue.poll();
            topologicalOrder[numExpanded++] = x;
            for (int y : graph.get(x)){
                if (--incomingEdges[y] == 0){
                    queue.offer(y);
                }
            }
        }
        return numExpanded == numCourses ? topologicalOrder : new int[]{};
    }
}

// TC: O(V+E)
// SC: O(V+E)

https://www.youtube.com/watch?v=Akt3glAwyfY&t=145s