sparknby Edworld Co.
/Problem Board & Sandbox
MediumAcceptance: 46.1%

Course Schedule & Dependency Graph

There are a total of `numCourses` courses you have to take, labeled from `0` to `numCourses - 1`. You are given an array `prerequisites` where `prerequisites[i] = [a_i, b_i]` indicates that you **must** take course `b_i` first if you want to take course `a_i`. Return `true` if you can finish all courses. Otherwise, return `false` (cycle detected in dependency graph).

Examples:
Input: numCourses = 2, prerequisites = [[1,0]]
Output: true
There are a total of 2 courses. To take course 1 you should have finished course 0. So it is possible.
Input: numCourses = 2, prerequisites = [[1,0],[0,1]]
Output: false
There are a total of 2 courses. To take course 1 you should have finished course 0, and to take course 0 you should have finished course 1. Circular dependency detected.
Constraints:
  • 1 <= numCourses <= 2000
  • 0 <= prerequisites.length <= 5000
  • prerequisites[i].length == 2
  • All the pairs prerequisites[i] are unique.
#Graph#BFS#DFS#Topological Sort
UTF-8 • 4 Spaces • TypeSafe

Click “Run Code” or “Submit” to execute the test suite against edge cases.