FunctionRacerProblemsRace

Everything Needed First

EasyGraphsPython, JavaScript, TypeScript, Java, C++

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

Race itor take your time

Write it in your browser, against the clock. The model solution, stepped through line by line, comes after.

More Graphs problems