Uber | Phone | Parallel Courses

4444
Feb 15, 2020
Feb 17, 2020

https://leetcode.com/problems/parallel-courses/ (premium)

Given K courses required to graduate and array of those courses. Some needs to be taken before others.
[i,j] - course i must be taken in a semester that is prior to course j.
You can take as many courses as you want every semester.

Find a minimum number of semesters you need to finish all K courses. If not possible - throw some exception or whatever.


Example 1:

3, [[1,0], [1,2]].

3 courses to graduate.
Course 1 must be take before course 0.
Course 1 must be taken before course 2.

First semester you take course 1. Second semester you take course 0 and 2.

Output: 2.

Example 2:

2, [[0,1], [1,0]

2 courses to graduate.
Course 0 must be taken before course 1.
Course 1 must be take before course 0.

Output:
Exception aka "not possible."

Comments (10)