Leetcode 207 Course Schedule Soln in Python
There are a total of n courses you have to take, labeled from 0 to n-1.
Some courses may have prerequisites, for example to take course 0 you have to first take course 1, which is expressed as a pair: [0,1]
Given the total number of courses and a list of prerequisite pairs, is it possible for you to finish all courses?
Approach - Its a graph question, we simply have to find if there is a cycle in the Graph. The graph is directed. We take three sets with names White, Grey and Black. White stores the unvisited nodes, grey stores the visited ones and black stores the node whose all children have been visited. If at any time we encounter a node in grey set, we know that there is a cycle in the graph. Below is the approach but make sure to do yourself first.
|
|