Course Schedule II (Leetcode 210) - Google Coding Question

Mani
Mani
Educating everyone with the beauty of programming!!

There are a total of n courses you have to take labelled from 0 to n - 1.

Some courses may have prerequisites, for example, if prerequisites[i] = [ai, bi] this means you must take the course bi before the course ai.

Given the total number of courses numCourses and a list of the prerequisite pairs, 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]

 

Constraints:

  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= numCourses * (numCourses - 1)
  • prerequisites[i].length == 2
  • 0 <= ai, bi < numCourses
  • ai != bi
  • All the pairs [ai, bi] are distinct.

Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/**
 * Time Complexity: O(V + E) : V is vertices, E is edges
 * Space Compexity: O(V + E)
 */
class Solution {
    public int[] findOrder(int numCourses, int[][] prerequisites) {
        buildGraph(numCourses, prerequisites);
        List<Integer> list = new ArrayList<>();
        for(int i =0 ; i < numCourses; i++) {
            dfs(i, list);
            if(cycle) {
                return new int[]{};
            }
        }
        Collections.reverse(list);
        return list.stream().mapToInt(i -> i).toArray();
    }

    Map<Integer, List<Integer>> map = new HashMap<>();
    Set<Integer> visited = new HashSet<>();
    Set<Integer> ongoing = new HashSet<>();
    boolean cycle = false;

    void dfs(int cur, List<Integer> res) {
        if(cycle) {
            return;
        }

        //This needs to be before the visited condition to detect cycle
        //or else this condition will never execute for visited
        if(ongoing.contains(cur)) {
            cycle = true;
            return;
        }

        if(visited.contains(cur)) {
            return;
        }

        visited.add(cur);
        ongoing.add(cur);
        for(int child : map.get(cur)) {
            dfs(child, res);
        }
        ongoing.remove(cur);
        res.add(cur);
    }


    void buildGraph(int n, int[][] courses) {
        for(int i = 0; i < n ; i++) {
            map.put(i, new ArrayList<>());
        }

        for(int[] course : courses) {
            map.get(course[1]).add(course[0]);
        }
    }


}
Drop your comments below for any questions or comments. For full list of must learn top interview questions Click Here: Must Learn Top Interview Questions

Rating: