-
-
Notifications
You must be signed in to change notification settings - Fork 610
/
EmployeeImportance.java
48 lines (41 loc) · 1.27 KB
/
EmployeeImportance.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
package problems.medium;
import java.util.*;
/**
* Simple graph traversal problem
*/
public class EmployeeImportance {
class Employee {
// It's the unique id of each node;
// unique id of this employee
public int id;
// the importance value of this employee
public int importance;
// the id of direct subordinates
public List<Integer> subordinates;
}
int total = 0;
public int getImportance(List<Employee> employees, int id) {
if (employees == null || employees.isEmpty())
return 0;
if (employees.size() == 1)
return employees.get(0).importance;
Set<Integer> visited = new HashSet<>();
visited.add(id);
Map<Integer, Employee> map = new HashMap<>();
for (Employee e : employees) {
map.put(e.id, e);
}
total = map.get(id).importance;
dfs(id, visited, map);
return total;
}
void dfs(int start, Set<Integer> visited, Map<Integer, Employee> map) {
for (Integer sub : map.get(start).subordinates) {
if (!visited.contains(sub)) {
total += map.get(sub).importance;
visited.add(sub);
dfs(sub, visited, map);
}
}
}
}