You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

49 lines
1.3 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package class_2022_04_2_week;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
// 来自美团
// 3.26笔试
// 给定一个正数n, 表示有0~n-1号任务
// 给定一个长度为n的数组timetime[i]表示i号任务做完的时间
// 给定一个二维数组matrix
// matrix[j] = {a, b} 代表a任务想要开始依赖b任务的完成
// 只要能并行的任务都可以并行,但是任何任务只有依赖的任务完成,才能开始
// 返回一个长度为n的数组ans表示每个任务完成的时间
// 输入可以保证没有循环依赖
public class Code04_AllJobFinishTime {
public static int[] finishTime(int n, int[] time, int[][] matrix) {
ArrayList<ArrayList<Integer>> nexts = new ArrayList<>();
for (int i = 0; i < n; i++) {
nexts.add(new ArrayList<>());
}
int[] in = new int[n];
for (int[] line : matrix) {
nexts.get(line[1]).add(line[0]);
in[line[0]]++;
}
Queue<Integer> zeroInQueue = new LinkedList<>();
int[] ans = new int[n];
for (int i = 0; i < n; i++) {
if (in[i] == 0) {
zeroInQueue.add(i);
}
}
while (!zeroInQueue.isEmpty()) {
int cur = zeroInQueue.poll();
ans[cur] += time[cur];
for (int next : nexts.get(cur)) {
ans[next] = Math.max(ans[next], ans[cur]);
if (--in[next] == 0) {
zeroInQueue.add(next);
}
}
}
return ans;
}
}