Skip to content

Commit ce3cadd

Browse files
committed
Added Time Needed to Inform All Employees.java
1 parent d402c14 commit ce3cadd

File tree

1 file changed

+23
-0
lines changed

1 file changed

+23
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
class Solution {
2+
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
3+
Map<Integer, List<Integer>> organization = new HashMap<>();
4+
for (int i = 0; i < manager.length; i++) {
5+
if (manager[i] != -1) {
6+
organization.computeIfAbsent(manager[i], k -> new ArrayList<>()).add(i);
7+
}
8+
}
9+
int totalTime = 0;
10+
Queue<int[]> queue = new LinkedList<>();
11+
queue.add(new int[]{headID, 0});
12+
while (!queue.isEmpty()) {
13+
int[] removed = queue.remove();
14+
int id = removed[0];
15+
int time = removed[1];
16+
totalTime = Math.max(totalTime, time);
17+
for (int associate : organization.getOrDefault(id, new ArrayList<>())) {
18+
queue.add(new int[]{associate, time + informTime[id]});
19+
}
20+
}
21+
return totalTime;
22+
}
23+
}

0 commit comments

Comments
 (0)