Can All the Courses Be Completed
Implement canFinishAll
A school offers n courses, and some courses can only be taken after others. The requirements are given as an adjacency list of a directed graph: courses[u] holds the courses that require course u first. Decide whether all courses can be completed.
This is equivalent to asking whether the graph has no cycle. Kahn's algorithm answers it by repeatedly taking courses whose requirements are all finished and checking whether every course ends up taken.
Example 1:
Input: courses = [[1,4],[2],[3],[1],[5],[]]
Output: false
Example 2:
Input: courses = [[1,2],[2],[]]
Output: true
Example 3:
Input: courses = [[0]]
Output: false
+ 14 hidden test cases run on Submit.
Constraints:
- ●
1 ≤ n ≤ 8 courses numbered 0 … n-1; courses[u] lists, in increasing order, every course v that requires course u to be completed first (adjacency-list form of a directed graph) - ●
No course is listed twice for the same u; a course may require itself (a road u → u), which can never be satisfied - ●
You take courses one at a time - ●
Return true if it is possible to complete all courses, otherwise false
courses =
[[1,4], [2], [3], [1], [5], []]