Medium

Quiz

#207 Course Schedule

APPROACH

You need to pass numCourses courses labeled 0 through numCourses - 1. The array prerequisites[i] = [ai, bi] means course bi must be completed before ai.

- For instance, [0, 1] means course 1 must come first.

Return true if completing all courses is achievable, or false if a dependency cycle makes it impossible.

Example 1:

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

Example 2:

Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
Explanation: There are a total of 2 courses to take.
To take course 1 you should have finished course 0, and to take course 0 you should also have finished course 1. So it is impossible.
1 of 4
1:00

What is the optimal approach for this problem?