Skip to content

Commit bffa1aa

Browse files
committed
Add Solution.java to problems 0690
1 parent ebf8283 commit bffa1aa

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Employee Importance
2+
3+
You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id.
4+
5+
For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.
6+
7+
Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.
8+
9+
10+
## Example 1:
11+
```text
12+
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
13+
14+
Output: 11
15+
16+
Explanation: Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
17+
```
18+
## Note:
19+
20+
- One employee has at most one direct leader and may have several subordinates.
21+
- The maximum number of employees won't exceed 2000.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
// Employee info
3+
class Employee {
4+
// It's the unique id of each node;
5+
// unique id of this employee
6+
public int id;
7+
// the importance value of this employee
8+
public int importance;
9+
// the id of direct subordinates
10+
public List<Integer> subordinates;
11+
};
12+
*/
13+
14+
import java.util.*;
15+
16+
class Solution {
17+
public int getImportance(List<Employee> employees, int id) {
18+
Map<Integer, Employee> map = new HashMap<>();
19+
for (Employee employee : employees) {
20+
map.put(employee.id, employee);
21+
}
22+
return dfs(map, id);
23+
}
24+
25+
private int dfs(Map<Integer, Employee> map, int id) {
26+
Employee employee = map.get(id);
27+
int ans = employee.importance;
28+
for (Integer subordinate : employee.subordinates) {
29+
ans += dfs(map, subordinate);
30+
}
31+
return ans;
32+
}
33+
}

0 commit comments

Comments
 (0)