Task Order

Asked at NetflixOnsite

Reported by the problem's creator, not verified

TaskGraph(n) manages n tasks labeled 0 to n - 1. add_dependency(before, after) states that task before must finish before task after can start. The same pair can be added more than once, and a task can depend on itself.

Examples

  1. Example 1

    Input: init = [5], operations = [["order",[]],["add_dependency",[3,1]],["add_dependency",[4,1]],["add_dependency",[1,0]],["order",[]],["add_dependency",[2,0]],["order",[]]]

    Output: [[0,1,2,3,4],null,null,null,[2,3,4,1,0],null,[2,3,4,1,0]]

    Explanation: With no dependencies the order is 0, 1, 2, 3, 4. After adding 3 -> 1, 4 -> 1 and 1 -> 0, tasks 2, 3 and 4 can go first, and 1 and 0 must wait: the smallest valid order is 2, 3, 4, 1, 0. Adding 2 -> 0 changes nothing.

Import your own questions
Paste a question you were asked and get a full problem: statement, tests and a checked solution.
Import a question

Code

Loading editor…

Ln 1, Col 1

Start an attempt to use Coach.