You are given n tasks labeled 0 to n - 1 and an array requirements where requirements[i] = [a, b] means task b must be completed before task a can start.
Return true if it is possible to complete all n tasks, and false otherwise. Completion is impossible exactly when some chain of requirements loops back on itself.
Examples
Example 1
Input: n = 2, requirements = [[1,0]]
Output: true
Explanation: Complete task 0 first, then task 1. Every requirement is satisfied.
Example 2
Input: n = 2, requirements = [[1,0],[0,1]]
Output: false
Explanation: Task 1 requires task 0 to be done first, and task 0 requires task 1 to be done first. The requirements are circular, so neither task can ever start.
Constraints
1≤n≤2000
0≤requirements.length≤5000
requirements[i].length=2
0≤a,b<n
All pairs requirements[i] are unique.
Loading editor...
Log in to run your code. Your draft is saved in this browser.Log inSign up