-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparallel_courses_iii.go
64 lines (56 loc) · 2.05 KB
/
parallel_courses_iii.go
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package problem2050
/*
You are given an integer n, which indicates that there are n courses labeled from 1 to n.
You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that
course prevCoursej has to be completed before course nextCoursej (prerequisite relationship).
Furthermore, you are given a 0-indexed integer array time where time[i] denotes
how many months it takes to complete the (i+1)th course.
You must find the minimum number of months needed to complete all the courses following these rules:
You may start taking a course at any time if the prerequisites are met.
Any number of courses can be taken at the same time.
Return the minimum number of months needed to complete all the courses.
Note: The test cases are generated such that it is possible to complete every course
(i.e., the graph is a directed acyclic graph).
*/
func minimumTime(n int, relations [][]int, time []int) int {
var res int
// graph[i] represents the courses the i'th course needs
var graph = make([][]int, n)
// fTime[i] represesnts the minimal time needed to complete the i'th course
var fTime = make([]int, n)
// solve(i) recursively finds the minimal time to complete the i'th course
var solve func(int) int
// Building the dependency graph
for _, r := range relations {
graph[r[1]-1] = append(graph[r[1]-1], r[0]-1)
}
solve = func(cur int) int {
var res int
// If we already found the minimal time, return it
if fTime[cur] != 0 {
return fTime[cur]
}
// Loop over all the course's dependencies
for _, d := range graph[cur] {
// Find out their minimal completion time
dTime := solve(d)
// Pick the maximum of those times, since you need to finish all of them
if dTime > res {
res = dTime
}
}
// Write to cache
fTime[cur] = res + time[cur]
return fTime[cur]
}
// Loop over all the courses
for i := range fTime {
// Find the minimal time for each
solve(i)
// Pick the maximum time (the last course that needs to be completed)
if fTime[i] > res {
res = fTime[i]
}
}
return res
}