-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
36 lines (34 loc) · 993 Bytes
/
Solution.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
/*
// Employee info
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;
};
*/
class Solution {
public int getImportance(List<Employee> employees, int id) {
int importance = 0;
List<Employee> stack = new ArrayList<Employee>();
for (Employee e : employees) {
if (e.id == id) {
stack.add(e);
}
}
while (stack.size() > 0) {
Employee employee = stack.get(stack.size() - 1);
stack.remove(stack.size() - 1);
importance += employee.importance;
for (Employee e : employees) {
if (employee.subordinates.contains(e.id)) {
stack.add(e);
}
}
}
return importance;
}
}