Skip to content

Commit 28567f3

Browse files
committed
added problem 2050
1 parent a1ff6ef commit 28567f3

File tree

3 files changed

+103
-0
lines changed

3 files changed

+103
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
package problem2050
2+
3+
/*
4+
You are given an integer n, which indicates that there are n courses labeled from 1 to n.
5+
You are also given a 2D integer array relations where relations[j] = [prevCoursej, nextCoursej] denotes that
6+
course prevCoursej has to be completed before course nextCoursej (prerequisite relationship).
7+
Furthermore, you are given a 0-indexed integer array time where time[i] denotes
8+
how many months it takes to complete the (i+1)th course.
9+
You must find the minimum number of months needed to complete all the courses following these rules:
10+
You may start taking a course at any time if the prerequisites are met.
11+
Any number of courses can be taken at the same time.
12+
Return the minimum number of months needed to complete all the courses.
13+
Note: The test cases are generated such that it is possible to complete every course
14+
(i.e., the graph is a directed acyclic graph).
15+
*/
16+
17+
func minimumTime(n int, relations [][]int, time []int) int {
18+
var res int
19+
// graph[i] represents the courses the i'th course needs
20+
var graph = make([][]int, n)
21+
// fTime[i] represesnts the minimal time needed to complete the i'th course
22+
var fTime = make([]int, n)
23+
// solve(i) recursively finds the minimal time to complete the i'th course
24+
var solve func(int) int
25+
26+
// Building the dependency graph
27+
for _, r := range relations {
28+
graph[r[1]-1] = append(graph[r[1]-1], r[0]-1)
29+
}
30+
31+
solve = func(cur int) int {
32+
var res int
33+
// If we already found the minimal time, return it
34+
if fTime[cur] != 0 {
35+
return fTime[cur]
36+
}
37+
38+
// Loop over all the course's dependencies
39+
for _, d := range graph[cur] {
40+
// Find out their minimal completion time
41+
dTime := solve(d)
42+
// Pick the maximum of those times, since you need to finish all of them
43+
if dTime > res {
44+
res = dTime
45+
}
46+
}
47+
48+
// Write to cache
49+
fTime[cur] = res + time[cur]
50+
return fTime[cur]
51+
}
52+
53+
// Loop over all the courses
54+
for i := range fTime {
55+
// Find the minimal time for each
56+
solve(i)
57+
// Pick the maximum time (the last course that needs to be completed)
58+
if fTime[i] > res {
59+
res = fTime[i]
60+
}
61+
}
62+
63+
return res
64+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package problem2050
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type TestCase struct {
11+
N int
12+
Relations [][]int
13+
Time []int
14+
Expected int
15+
}
16+
17+
var TestCases = []TestCase{
18+
{3, [][]int{{1, 3}, {2, 3}}, []int{3, 2, 5}, 8},
19+
{5, [][]int{{1, 5}, {2, 5}, {3, 5}, {3, 4}, {4, 5}}, []int{1, 2, 3, 4, 5}, 12},
20+
}
21+
22+
func TestParallelCoursesIII(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, tc := range TestCases {
26+
want := tc.Expected
27+
got := minimumTime(tc.N, tc.Relations, tc.Time)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", tc))
29+
}
30+
}
31+
32+
func BenchmarkVParallelCoursesIII(b *testing.B) {
33+
for _, tc := range TestCases {
34+
for i := 0; i < b.N; i++ {
35+
minimumTime(tc.N, tc.Relations, tc.Time)
36+
}
37+
}
38+
}

readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -533,6 +533,7 @@ Each problem is in it's own directory, with test files. There are helper package
533533
| 2017 | [Grid Game](https://leetcode.com/problems/grid-game) | [My Solution](./problems/problem2017) ||
534534
| 2024 | [Maximize the Confusion of an Exam](https://leetcode.com/problems/maximize-the-confusion-of-an-exam) | [My Solution](./problems/problem2024) ||
535535
| 2038 | [Remove Colored Pieces if Both Neighbors are the Same Color](https://leetcode.com/problems/remove-colored-pieces-if-both-neighbors-are-the-same-color) | [My Solution](./problems/problem2038) ||
536+
| 2050 | [Parallel Courses III](https://leetcode.com/problems/parallel-courses-iii) | [My Solution](./problems/problem2050) ||
536537
| 2070 | [Most Beautiful Item for Each Query](https://leetcode.com/problems/most-beautiful-item-for-each-query) | [My Solution](./problems/problem2070) ||
537538
| 2090 | [K Radius Subarray Averages](https://leetcode.com/problems/k-radius-subarray-averages) | [My Solution](./problems/problem2090) ||
538539
| 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list) | [My Solution](./problems/problem2095) ||

0 commit comments

Comments
 (0)