Loading problem…
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] = [ai, bi] indicates that you must take course bi first if you want to take course ai.
For example, the pair [0, 1], indicates that to take course 0 you have to first take course 1.
Return true if you can finish all courses. Otherwise, return false.
This problem is about detecting cycles in a directed graph. If there's a cycle in the prerequisites (e.g., course A requires course B, and course B requires course A), it's impossible to complete all courses.
canFinish(2, [[1,0]]); // true - course 1 requires course 0, no cycle
canFinish(2, [[1,0],[0,1]]); // false - circular dependency
canFinish(3, [[1,0],[2,1]]); // true - linear chain, no cycleThis problem models real course scheduling systems: