STATUS
• Solved
• Unsolved

Showing:
Handle Score
@Ibrahim Nash 6420
@mb1973 5594
@Quandray 5231
@akhayrutdinov 5111
@saiujwal13083 4510
@sanjay05 3762
@kirtidee18 3673
@marius_valentin_dragoi 3522
@sushant_a 3459
@verma_ji 3412
Medium Accuracy: 52.81% Submissions: 2249 Points: 4

There are a total of N tasks, labeled from 0 to N-1. Some tasks may have prerequisites, for example to do task 0 you have to first complete task 1, which is expressed as a pair: [0, 1]
Given the total number of tasks N and a list of prerequisite pairs P, find if it is possible to finish all tasks.

Example 1:

Input:
N = 4, P = 3
prerequisites = {{1,0},{2,1},{3,2}}
Output:
Yes
Explanation:
To do task 1 you should have completed
should have finished task 2. So it is possible.


Example 2:

Input:
N = 2, P = 2
prerequisites = {{1,0},{0,1}}
Output:
3
Explanation:
To do task 1 you should have completed
have finished task 1. So it is impossible.


You don’t need to read input or print anything. Your task is to complete the function isPossible() which takes the integer N denoting the number of tasks, P denoting the number of prerequisite pairs and prerequisite as input parameters and returns true
if it is possible to finish all tasks otherwise returns false.

Expected Time Complexity: O(N + P)
Expected Auxiliary Space: O(N + P)

Constraints:
1 ≤ N, P ≤ 100

Editorial

We strongly recommend solving this problem on your own before viewing its editorial. Do you still want to view the editorial?