You are given a data structure of employee information, which includes the employee's unique id, his importance value and his direct subordinates' id.
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.
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.
Example 1:
Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
Output: 11
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.
首先复习一下HashMap和Queue的常用方法
HashMap
SN 方法及描述
1 void clear(): 移除所有映射关系
2 Object clone(): 返回此HashMap实例的浅表副本:键和值本身不被复制。
3 boolean containsKey(Object key): 判断是否包含键
4 boolean containsValue(Object value) : 判断是否包含值
5 int size(): 返回数量
6 Object get(Object key): 返回指定键的值或者null
7 boolean isEmpty(): 判断是否为空
8 Set keySet(): 返回键的set
9 Object put(Object key, Object value): 关联与指定键的指定的值
10 putAll(Map m): 复制所有指定映射到此映射,这些映射关系将替换所有当前映射中键的映射关系
11 Object remove(Object key): 移除映射
12 Collection values(): 返回值的collection
Queue
队列是一种特殊的线性表,它只允许在表的前端进行删除操作,而在表的后端进行插入操作。
LinkedList类实现了Queue接口,因此我们可以把LinkedList当成Queue来用。
1 offer(): 添加元素
2 poll(): 返回第一个元素,并在队列中删除
3 element(): 返回第一个元素
4 peek(): 返回第一个元素
最简单做法
class Solution {
public int getImportance(List<Employee> employees, int id) {
Employee leader = employees.get(id-1);
int importance = leader.importance;
List<Integer> subordinates = leader.subordinates;
for(Integer i : subordinates){
importance += getImportance(employees,i);
}
return importance;
}
}
另一种思路,用hashMap将id与其对应的数据结构映射起来,可以通过两种方法去解,分别是BFS和DFS
BFS
/*
// 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 total = 0;
Map<Integer,Employee> map = new HashMap<>();
for(Employee employee: employees){
map.put(employee.id, employee);
}
Queue<Employee> queue = new LinkedList<>();
queue.offer(map.get(id));
while(!queue.isEmpty()){
Employee current = queue.poll();
total += current.importance;
for (int subordinate: current.subordinates){
queue.offer(map.get(subordinate));
}
}
return total;
}
}
DFS
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer,Employee> map = new HashMap<Integer,Employee>();
for(int i=0;i<employees.size();i++)
map.put(employees.get(i).id,employees.get(i));
return getSubImportance(map,id);
}
static int getSubImportance(Map<Integer,Employee> map,int i){
Employee leader = map.get(i);
int importance = leader.importance;
List<Integer> subordinates = leader.subordinates;
for(Integer j:subordinates)
importance = importance + getSubImportance(map,j);
return importance;
}
}
网友评论