A house renovation has n jobs numbered 1 .. n, and each pair [a, b] in relations says job a must be finished on an earlier day than job b. On any day the crew can do as many jobs as they like, provided every job they do had all its prerequisites finished on earlier days. (The function calls each day a round.)
Return the fewest days needed to finish every job, or -1 if some jobs wait on each other in a loop so the renovation can never be finished.
Examples
Input: n = 5, relations = [[2, 5], [1, 5], [5, 3]]
Output: 3
Explanation: day 1: jobs 1, 2 and 4; day 2: job 5; day 3: job 3.
Input: n = 4, relations = [[1, 2], [2, 4], [4, 2]]
Output: -1
Explanation: jobs 2 and 4 each wait for the other, so neither can ever be done.
Constraints
1 <= n <= 10**4,0 <= len(relations) <= 3 * 10**4- Target: O(V + E) time.
Goals
- Process a DAG level by level and count the levels
- Return a sentinel when a cycle makes the schedule impossible