Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
zz-cpp
algorithm-journey
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
main
Breadcrumbs
algorithm-journey
/
src
/
class060
/
Code03_ParallelCoursesIII.java
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
56 lines (52 loc) · 1.82 KB
main
Breadcrumbs
algorithm-journey
/
src
/
class060
/
Code03_ParallelCoursesIII.java
Copy path
Top
File metadata and controls
Code
Blame
56 lines (52 loc) · 1.82 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
class060
;
import
java
.
util
.
ArrayList
;
// 并行课程 III
// 给你一个整数 n ,表示有 n 节课,课程编号从 1 到 n
// 同时给你一个二维整数数组 relations ,
// 其中 relations[j] = [prevCoursej, nextCoursej]
// 表示课程 prevCoursej 必须在课程 nextCoursej 之前 完成(先修课的关系)
// 同时给你一个下标从 0 开始的整数数组 time
// 其中 time[i] 表示完成第 (i+1) 门课程需要花费的 月份 数。
// 请你根据以下规则算出完成所有课程所需要的 最少 月份数:
// 如果一门课的所有先修课都已经完成,你可以在 任意 时间开始这门课程。
// 你可以 同时 上 任意门课程 。请你返回完成所有课程所需要的 最少 月份数。
// 注意:测试数据保证一定可以完成所有课程(也就是先修课的关系构成一个有向无环图)
// 测试链接 : https://leetcode.cn/problems/parallel-courses-iii/
public
class
Code03_ParallelCoursesIII
{
public
static
int
minimumTime
(
int
n
,
int
[][]
relations
,
int
[]
time
) {
// 点 : 1....n
ArrayList
<
ArrayList
<
Integer
>>
graph
=
new
ArrayList
<>();
for
(
int
i
=
0
;
i
<=
n
;
i
++) {
graph
.
add
(
new
ArrayList
<>());
}
int
[]
indegree
=
new
int
[
n
+
1
];
for
(
int
[]
edge
:
relations
) {
graph
.
get
(
edge
[
0
]).
add
(
edge
[
1
]);
indegree
[
edge
[
1
]]++;
}
int
[]
queue
=
new
int
[
n
];
int
l
=
0
;
int
r
=
0
;
for
(
int
i
=
1
;
i
<=
n
;
i
++) {
if
(
indegree
[
i
] ==
0
) {
queue
[
r
++] =
i
;
}
}
int
[]
cost
=
new
int
[
n
+
1
];
int
ans
=
0
;
while
(
l
<
r
) {
int
cur
=
queue
[
l
++];
// 1 : time[0]
// x : time[x-1]
cost
[
cur
] +=
time
[
cur
-
1
];
ans
=
Math
.
max
(
ans
,
cost
[
cur
]);
for
(
int
next
:
graph
.
get
(
cur
)) {
cost
[
next
] =
Math
.
max
(
cost
[
next
],
cost
[
cur
]);
if
(--
indegree
[
next
] ==
0
) {
queue
[
r
++] =
next
;
}
}
}
return
ans
;
}
}
You can’t perform that action at this time.