Task scheduling semesters

Write codeMedium25 minAmazon style

Your task

There are n tasks labeled 0..n-1 and a list of prerequisite pairs [a, b] meaning b must be finished before a. Each semester you may take any number of tasks whose prerequisites are all already finished. Return the minimum number of semesters needed to finish all tasks, or -1 if a cycle makes it impossible. With no prerequisites everything can be done in one semester.

Examples

min_semesters(n: int, prereqs: list[list[int]]) -> int
Example 1
Inputmin_semesters(4, [[1, 0], [2, 0], [3, 1], [3, 2]])
Output3