-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathParallel_Courses_III.py
34 lines (27 loc) · 1.45 KB
/
Parallel_Courses_III.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#Question: Given a list of courses and their prerequisites, and time needed to complete each course, find the min time to complete all courses
#Solution: Use Khans algorithm to perform a top sort, updating a store of time needed for each course,
# the time for each course is the max of itself or the time needed to complete the previous course + time for this course
#Difficulty: Hard
from typing import List
from collections import defaultdict
def minimumTime(n: int, relations: List[List[int]], time: List[int]) -> int:
indegrees = {}
graph = defaultdict(list)
timeToCourse = [0] + time
for prev, nxt in relations:
indegrees[prev] = 0
indegrees[nxt] = 0
for prev, nxt in relations:
indegrees[nxt] += 1
graph[prev].append(nxt)
# get all courses without any prerequisites
q = [course for course, ins in indegrees.items() if ins == 0]
while q:
course = q.pop()
for nxt in graph[course]:
# time to the next course is either the max of itself (ie we got to the nxt course through another path in the graph), or this path, which is
# currentCourseTime + timeToPrevious course takes more time, the max of either is the bottleneck
timeToCourse[nxt] = max(timeToCourse[nxt], timeToCourse[course] + time[nxt-1])
indegrees[nxt] -= 1
if indegrees[nxt] == 0: q.append(nxt)
return max(timeToCourse)