Skip to content

Commit 9219c26

Browse files
committed
feat: add solutions to lc problem: No.2050
No.2050.Parallel Courses III
1 parent 908bf64 commit 9219c26

File tree

6 files changed

+433
-2
lines changed

6 files changed

+433
-2
lines changed

solution/2000-2099/2050.Parallel Courses III/README.md

+150-1
Original file line numberDiff line numberDiff line change
@@ -67,22 +67,171 @@
6767

6868
<!-- 这里可写通用的实现逻辑 -->
6969

70+
**方法一:拓扑排序 + 动态规划**
71+
72+
定义 $dp[i]$ 表示完成第 $i$ 门课程需要花费的最少月份数。
73+
7074
<!-- tabs:start -->
7175

7276
### **Python3**
7377

7478
<!-- 这里可写当前语言的特殊实现逻辑 -->
7579

7680
```python
77-
81+
class Solution:
82+
def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
83+
g = defaultdict(list)
84+
indeg = [0] * n
85+
for a, b in relations:
86+
g[a - 1].append(b - 1)
87+
indeg[b - 1] += 1
88+
q = deque()
89+
dp = [0] * n
90+
ans = 0
91+
for i, (v, t) in enumerate(zip(indeg, time)):
92+
if v == 0:
93+
q.append(i)
94+
dp[i] = t
95+
ans = max(ans, t)
96+
while q:
97+
i = q.popleft()
98+
for j in g[i]:
99+
dp[j] = max(dp[j], dp[i] + time[j])
100+
ans = max(ans, dp[j])
101+
indeg[j] -= 1
102+
if indeg[j] == 0:
103+
q.append(j)
104+
return ans
78105
```
79106

80107
### **Java**
81108

82109
<!-- 这里可写当前语言的特殊实现逻辑 -->
83110

84111
```java
112+
class Solution {
113+
public int minimumTime(int n, int[][] relations, int[] time) {
114+
List<Integer>[] g = new List[n];
115+
for (int i = 0; i < n; ++i) {
116+
g[i] = new ArrayList<>();
117+
}
118+
int[] indeg = new int[n];
119+
for (int[] e : relations) {
120+
int a = e[0] - 1, b = e[1] - 1;
121+
g[a].add(b);
122+
++indeg[b];
123+
}
124+
Deque<Integer> q = new ArrayDeque<>();
125+
int[] dp = new int[n];
126+
int ans = 0;
127+
for (int i = 0; i < n; ++i) {
128+
int v = indeg[i], t = time[i];
129+
if (v == 0) {
130+
q.offer(i);
131+
dp[i] = t;
132+
ans = Math.max(ans, t);
133+
}
134+
}
135+
while (!q.isEmpty()) {
136+
int i = q.pollFirst();
137+
for (int j : g[i]) {
138+
dp[j] = Math.max(dp[j], dp[i] + time[j]);
139+
ans = Math.max(ans, dp[j]);
140+
if (--indeg[j] == 0) {
141+
q.offer(j);
142+
}
143+
}
144+
}
145+
return ans;
146+
}
147+
}
148+
```
149+
150+
### **C++**
151+
152+
```cpp
153+
class Solution {
154+
public:
155+
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
156+
vector<vector<int>> g(n);
157+
vector<int> indeg(n);
158+
for (auto& e : relations)
159+
{
160+
int a = e[0] - 1, b = e[1] - 1;
161+
g[a].push_back(b);
162+
++indeg[b];
163+
}
164+
queue<int> q;
165+
vector<int> dp(n);
166+
int ans = 0;
167+
for (int i = 0; i < n; ++i)
168+
{
169+
int v = indeg[i], t = time[i];
170+
if (v == 0)
171+
{
172+
q.push(i);
173+
dp[i] = t;
174+
ans = max(ans, t);
175+
}
176+
}
177+
while (!q.empty())
178+
{
179+
int i = q.front();
180+
q.pop();
181+
for (int j : g[i])
182+
{
183+
if (--indeg[j] == 0) q.push(j);
184+
dp[j] = max(dp[j], dp[i] + time[j]);
185+
ans = max(ans, dp[j]);
186+
}
187+
}
188+
return ans;
189+
}
190+
};
191+
```
85192
193+
### **Go**
194+
195+
```go
196+
func minimumTime(n int, relations [][]int, time []int) int {
197+
g := make([][]int, n)
198+
indeg := make([]int, n)
199+
for _, e := range relations {
200+
a, b := e[0]-1, e[1]-1
201+
g[a] = append(g[a], b)
202+
indeg[b]++
203+
}
204+
dp := make([]int, n)
205+
q := []int{}
206+
ans := 0
207+
for i, v := range indeg {
208+
if v == 0 {
209+
q = append(q, i)
210+
dp[i] = time[i]
211+
ans = max(ans, time[i])
212+
}
213+
}
214+
for len(q) > 0 {
215+
i := q[0]
216+
q = q[1:]
217+
for _, j := range g[i] {
218+
indeg[j]--
219+
if indeg[j] == 0 {
220+
q = append(q, j)
221+
}
222+
dp[j] = max(dp[j], dp[i]+time[j])
223+
ans = max(ans, dp[j])
224+
}
225+
}
226+
return ans
227+
}
228+
229+
func max(a, b int) int {
230+
if a > b {
231+
return a
232+
}
233+
return b
234+
}
86235
```
87236

88237
### **...**

solution/2000-2099/2050.Parallel Courses III/README_EN.md

+146-1
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,158 @@ Thus, the minimum time needed to complete all the courses is 7 + 5 = 12 months.
6666
### **Python3**
6767

6868
```python
69-
69+
class Solution:
70+
def minimumTime(self, n: int, relations: List[List[int]], time: List[int]) -> int:
71+
g = defaultdict(list)
72+
indeg = [0] * n
73+
for a, b in relations:
74+
g[a - 1].append(b - 1)
75+
indeg[b - 1] += 1
76+
q = deque()
77+
dp = [0] * n
78+
ans = 0
79+
for i, (v, t) in enumerate(zip(indeg, time)):
80+
if v == 0:
81+
q.append(i)
82+
dp[i] = t
83+
ans = max(ans, t)
84+
while q:
85+
i = q.popleft()
86+
for j in g[i]:
87+
dp[j] = max(dp[j], dp[i] + time[j])
88+
ans = max(ans, dp[j])
89+
indeg[j] -= 1
90+
if indeg[j] == 0:
91+
q.append(j)
92+
return ans
7093
```
7194

7295
### **Java**
7396

7497
```java
98+
class Solution {
99+
public int minimumTime(int n, int[][] relations, int[] time) {
100+
List<Integer>[] g = new List[n];
101+
for (int i = 0; i < n; ++i) {
102+
g[i] = new ArrayList<>();
103+
}
104+
int[] indeg = new int[n];
105+
for (int[] e : relations) {
106+
int a = e[0] - 1, b = e[1] - 1;
107+
g[a].add(b);
108+
++indeg[b];
109+
}
110+
Deque<Integer> q = new ArrayDeque<>();
111+
int[] dp = new int[n];
112+
int ans = 0;
113+
for (int i = 0; i < n; ++i) {
114+
int v = indeg[i], t = time[i];
115+
if (v == 0) {
116+
q.offer(i);
117+
dp[i] = t;
118+
ans = Math.max(ans, t);
119+
}
120+
}
121+
while (!q.isEmpty()) {
122+
int i = q.pollFirst();
123+
for (int j : g[i]) {
124+
dp[j] = Math.max(dp[j], dp[i] + time[j]);
125+
ans = Math.max(ans, dp[j]);
126+
if (--indeg[j] == 0) {
127+
q.offer(j);
128+
}
129+
}
130+
}
131+
return ans;
132+
}
133+
}
134+
```
135+
136+
### **C++**
137+
138+
```cpp
139+
class Solution {
140+
public:
141+
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
142+
vector<vector<int>> g(n);
143+
vector<int> indeg(n);
144+
for (auto& e : relations)
145+
{
146+
int a = e[0] - 1, b = e[1] - 1;
147+
g[a].push_back(b);
148+
++indeg[b];
149+
}
150+
queue<int> q;
151+
vector<int> dp(n);
152+
int ans = 0;
153+
for (int i = 0; i < n; ++i)
154+
{
155+
int v = indeg[i], t = time[i];
156+
if (v == 0)
157+
{
158+
q.push(i);
159+
dp[i] = t;
160+
ans = max(ans, t);
161+
}
162+
}
163+
while (!q.empty())
164+
{
165+
int i = q.front();
166+
q.pop();
167+
for (int j : g[i])
168+
{
169+
if (--indeg[j] == 0) q.push(j);
170+
dp[j] = max(dp[j], dp[i] + time[j]);
171+
ans = max(ans, dp[j]);
172+
}
173+
}
174+
return ans;
175+
}
176+
};
177+
```
75178
179+
### **Go**
180+
181+
```go
182+
func minimumTime(n int, relations [][]int, time []int) int {
183+
g := make([][]int, n)
184+
indeg := make([]int, n)
185+
for _, e := range relations {
186+
a, b := e[0]-1, e[1]-1
187+
g[a] = append(g[a], b)
188+
indeg[b]++
189+
}
190+
dp := make([]int, n)
191+
q := []int{}
192+
ans := 0
193+
for i, v := range indeg {
194+
if v == 0 {
195+
q = append(q, i)
196+
dp[i] = time[i]
197+
ans = max(ans, time[i])
198+
}
199+
}
200+
for len(q) > 0 {
201+
i := q[0]
202+
q = q[1:]
203+
for _, j := range g[i] {
204+
indeg[j]--
205+
if indeg[j] == 0 {
206+
q = append(q, j)
207+
}
208+
dp[j] = max(dp[j], dp[i]+time[j])
209+
ans = max(ans, dp[j])
210+
}
211+
}
212+
return ans
213+
}
214+
215+
func max(a, b int) int {
216+
if a > b {
217+
return a
218+
}
219+
return b
220+
}
76221
```
77222

78223
### **...**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
class Solution {
2+
public:
3+
int minimumTime(int n, vector<vector<int>>& relations, vector<int>& time) {
4+
vector<vector<int>> g(n);
5+
vector<int> indeg(n);
6+
for (auto& e : relations)
7+
{
8+
int a = e[0] - 1, b = e[1] - 1;
9+
g[a].push_back(b);
10+
++indeg[b];
11+
}
12+
queue<int> q;
13+
vector<int> dp(n);
14+
int ans = 0;
15+
for (int i = 0; i < n; ++i)
16+
{
17+
int v = indeg[i], t = time[i];
18+
if (v == 0)
19+
{
20+
q.push(i);
21+
dp[i] = t;
22+
ans = max(ans, t);
23+
}
24+
}
25+
while (!q.empty())
26+
{
27+
int i = q.front();
28+
q.pop();
29+
for (int j : g[i])
30+
{
31+
if (--indeg[j] == 0) q.push(j);
32+
dp[j] = max(dp[j], dp[i] + time[j]);
33+
ans = max(ans, dp[j]);
34+
}
35+
}
36+
return ans;
37+
}
38+
};

0 commit comments

Comments
 (0)