Problem Submissions Solution

Course Schedule

Difficulty: Medium

Acceptance: %

Points: 30.00

You are given n tasks, labeled from 0 to n-1, and a list of m prerequisite pairs. Each pair [a, b] indicates that task a depends on task b and must be completed after it. Your goal is to determine an order in which to complete all the tasks.

  • There may be multiple valid orders; return any one of them.
  • If it is impossible to complete all tasks due to circular dependencies, return an empty array. The driver code will print "No Ordering Possible" when this happens.
  • If a valid order is returned, the output will be 1. If the order is invalid, the output will be 0.

The task is to complete the function findOrder() which takes two integers n, and m and a list of lists of size m*2 denoting the prerequisite pairs as input and returns any correct order to finish all the tasks. Return an empty array if it's impossible to finish all tasks.

Topics

Companies

Articles

Examples:

Expected Time Complexity: O(n+m).

Expected Auxiliary Space: O(n+m).

Constraints:
  • 1 = n = 10^5
  • 0 = m = min(n*(n-1),10^5)
  • 0 = prerequisites[i][0], prerequisites[i][1] < n
  • All prerequisite pairs are unique
  • prerequisites[i][0] ? prerequisites[i][1]
Companies:
Google
Topics:
Graph BFS DFS
Locked Content
Access Restricted: Please Login to access the code editor and test cases.