1015. Course Schedule I — Can Finish All Courses

EasyGraphsTopological SortDFSCycle

There are n courses labeled 0..n-1. Each directed edge [a, b] means course b must be taken before course a. Determine whether it is possible to finish all courses (i.e. the prerequisite graph has no cycle). Return true or false. The input is JSON {n, edges}.

Input: JSON {n, edges}.

Output: Boolean — true or false.

Examples

Example 1
Input: {"n":2,"edges":[[1,0]]}
Output: true
Explanation: Take 0 then 1.
Example 2
Input: {"n":2,"edges":[[1,0],[0,1]]}
Output: false
Explanation: Circular prerequisites.
Example 3
Input: {"n":1,"edges":[]}
Output: true
Explanation: No prerequisites.

Constraints

Asked by

TCSInfosysWiproCognizantCapgemini
Solve this problem in the editor →