There are numCourses courses, numbered 0 to numCourses - 1. Each prerequisites[i] = [a, b] means course b must be finished before you can take course a.
Return true if it is possible to finish every course, and false otherwise.
Take course 0, then 1, then 2.
Course 0 waits on 1, 1 waits on 2, and 2 waits on 0: none of them can ever start.
Constraints
1 <= numCourses <= 20000 <= prerequisites.length <= 5000prerequisites[i].length == 20 <= a, b < numCoursesDraw each course as a node with an arrow from a prerequisite to the course it unlocks. Every course can be finished exactly when there is no cycle.
Draw every course as a dot, and turn each pair [a, b] into an arrow from b to a: "take b, and then a opens up". The question becomes a question about that picture.
Time O(n + m)Space O(n + m)
The full walkthrough and code are part of Premium
true