Task Scheduling 2

Prereq: Topological Sort

This problem is similar to Task Scheduling. The primary difference is now we assign times to tasks and we ask for the minimum amount of time to complete all tasks. There will be an extra times array such that times[i] indicates the time required to complete task[i]. You have also invited all your friends to help complete your tasks so you can work on any amount of tasks at a given time. Remember that task a must be completed before completing task b (but the starting time don't need to be in order).

There is guaranteed to be a solution.


Example 1

1tasks = ["a", "b", "c", "d"]
2times = [1, 1, 2, 1]
3requirements = [["a", "b"], ["c", "b"], ["b", "d"]]
Output: 4

Try it yourself


Since this problem is similar to Task Scheduling, we still use the template for Kahn's Algorithm. The basic idea is to perform a topological sort except we also keep track of the largest amount of time expended for a particular path through the graph. The time it takes to complete a task is inhibited by the most time-consuming task in its dependencies. We make sure that we update every node such that it contains the maximum time needed to complete every prerequisite task. We then make sure to update every children such that it contains the distance as the final answer is essentially the maximal path through the graph.

Time Complexity: O(n+m)

The time complexity is equal to n the number of nodes in the graph plus m the number of edges in the graph. This is because we have to go through every connection and node once when we sort the graph.

Space Complexity: O(n)

The queue holds at most n nodes in the worst case.



Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.

Contrary to popular belief, Lorem Ipsum is not simply random text.

1  >>> a = [1, 2, 3]
2  >>> a[-1]
3  3

Get premium for instant access to all content and solutions