Everything Needed First
A degree has n courses numbered 0 to n - 1. requires is a list of pairs [a, b] meaning course a must be passed before course b can be taken. The same pair may appear more than once, and the requirements never run in a circle: no course requires itself, directly or through other courses.
Return how many different courses must be passed before course can be taken: its direct requirements, the requirements of those, and so on. Return 0 when course has no requirement.
Example 1
n 5
requires [[0,1],[1,3],[2,3],[3,4]]
course 4
returns 4
Course 4 needs 3, which needs 1 and 2, and 1 needs 0: four courses in all.
Example 2
n 4
requires [[0,1],[0,2]]
course 0
returns 0
Course 0 is required by others but requires nothing itself.
Constraints
1 <= n <= 500 <= len(requires) <= 1000 <= a, b <= n - 1anda != b- the requirements contain no cycle
0 <= course <= n - 1
Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.