-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1376TimeNeededToInformAllEmployees.java
More file actions
39 lines (36 loc) · 1.32 KB
/
1376TimeNeededToInformAllEmployees.java
File metadata and controls
39 lines (36 loc) · 1.32 KB
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
//https://leetcode.com/problems/time-needed-to-inform-all-employees/
class Solution {
public int numOfMinutes(int n, int headID, int[] manager, int[] informTime) {
Map<Integer, List<Integer>> map = new HashMap();
for(int i = 0; i < n; i++) {
map.put(i, new ArrayList());
}
for(int i = 0; i < n; i++) {
if(manager[i] != -1) {
map.get(manager[i]).add(i);
}
}
return dfs(headID, map, informTime, 0);
//return bfs(headID, map, informTime);
}
int dfs(int id, Map<Integer, List<Integer>> map, int[] informTime, int time) {
int maxTime = time;
for(Integer employeeId : map.get(id)) {
maxTime = Math.max(maxTime, dfs(employeeId, map, informTime, time + informTime[id]));
}
return maxTime;
}
int bfs(int id, Map<Integer, List<Integer>> map, int[] informTime) {
Queue<Integer[]> queue = new LinkedList();
queue.offer(new Integer[]{id, 0});
int time = 0;
while(!queue.isEmpty()) {
Integer[] top = queue.poll();
time = Math.max(time, top[1]);
for(Integer empId : map.get(top[0])) {
queue.offer(new Integer[]{empId, top[1] + informTime[top[0]]});
}
}
return time;
}
}