-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
/
Copy pathSolution.java
34 lines (34 loc) · 1014 Bytes
/
Solution.java
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
class Solution {
public int minimumTime(int n, int[][] relations, int[] time) {
List<Integer>[] g = new List[n];
Arrays.setAll(g, k -> new ArrayList<>());
int[] indeg = new int[n];
for (int[] e : relations) {
int a = e[0] - 1, b = e[1] - 1;
g[a].add(b);
++indeg[b];
}
Deque<Integer> q = new ArrayDeque<>();
int[] dp = new int[n];
int ans = 0;
for (int i = 0; i < n; ++i) {
int v = indeg[i], t = time[i];
if (v == 0) {
q.offer(i);
dp[i] = t;
ans = Math.max(ans, t);
}
}
while (!q.isEmpty()) {
int i = q.pollFirst();
for (int j : g[i]) {
dp[j] = Math.max(dp[j], dp[i] + time[j]);
ans = Math.max(ans, dp[j]);
if (--indeg[j] == 0) {
q.offer(j);
}
}
}
return ans;
}
}