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.

Examples

Example 1

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

Try it yourself

Solution

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.

←
↑TA πŸ‘¨β€πŸ«