-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path690EmployeeImportance.java
More file actions
43 lines (39 loc) · 1.15 KB
/
690EmployeeImportance.java
File metadata and controls
43 lines (39 loc) · 1.15 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
40
41
42
43
//https://leetcode.com/problems/employee-importance/
/*
// Definition for Employee.
class Employee {
public int id;
public int importance;
public List<Integer> subordinates;
};
*/
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> map = new HashMap();
for(Employee emp : employees) {
map.put(emp.id, emp);
}
//return dfs(id, map);
return bfs(id, map);
}
int dfs(Integer id, Map<Integer, Employee> map) {
int currentSum = map.get(id).importance;
for(Integer subordinate : map.get(id).subordinates) {
currentSum += dfs(subordinate, map);
}
return currentSum;
}
int bfs(Integer id, Map<Integer, Employee> map) {
Queue<Integer> queue = new LinkedList();
queue.add(id);
int sum = 0;
while(!queue.isEmpty()) {
Employee employee = map.get(queue.remove());
sum += employee.importance;
for(Integer subordinate : employee.subordinates) {
queue.add(subordinate);
}
}
return sum;
}
}