-
Notifications
You must be signed in to change notification settings - Fork 1
/
TimeNeededToInformAllEmployees.java
67 lines (54 loc) · 1.69 KB
/
TimeNeededToInformAllEmployees.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package com.smlnskgmail.jaman.leetcodejava.medium;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
// https://leetcode.com/problems/time-needed-to-inform-all-employees/
public class TimeNeededToInformAllEmployees {
private final int n;
private final int headID;
private final int[] manager;
private final int[] informTime;
public TimeNeededToInformAllEmployees(
int n,
int headID,
int[] manager,
int[] informTime
) {
this.n = n;
this.headID = headID;
this.manager = manager;
this.informTime = informTime;
}
public int solution() {
Graph graph = new Graph(manager, n);
return graph.getNumsOfMinutes(headID, informTime);
}
static class Graph {
List<List<Integer>> list;
int nodes;
Graph(int[] manager, int nodes) {
list = new ArrayList<>();
this.nodes = nodes;
for (int node = 0; node < nodes; node++) {
list.add(new LinkedList<>());
}
for (int reportee = 0; reportee < nodes; reportee++) {
int r = manager[reportee];
if (r == -1) {
continue;
}
list.get(r).add(reportee);
}
}
public int getNumsOfMinutes(int nodeId, int[] informTime) {
int result = 0;
for (int node : list.get(nodeId)) {
result = Math.max(
result,
informTime[nodeId] + getNumsOfMinutes(node, informTime)
);
}
return result;
}
}
}