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.
Note:
- One employee has at most one direct leader and may have several subordinates.
- The maximum number of employees won't exceed 2000.
最基本的DFS可以做,如果subordinate为空,直接返回importance值,如果不为空,就计算每一个subordinate的值,最后加起来。
DFS函数可以设计成有返回值的,也可以设计成没有返回值的,算法本质没有什么区别,没有返回值的只是利用了C++ pass by reference的性质。
下边的代码是有返回值的函数,并且将员工信息收录到了一个map中方便后边查询。
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*> employees_map;
for (Employee* e: employees) {
employees_map[e->id] = e;
}
return DFS(employees_map[id], employees_map);
}
int DFS(Employee* employee, unordered_map<int, Employee*> employees_map) {
if (employee->subordinates.size() == 0) {
return employee->importance;
}
int sum = 0;
sum += employee->importance;
for (int i = 0; i < employee->subordinates.size(); i++) {
sum += DFS(employees_map[employee->subordinates[i]], employees_map);
}
return sum;
}
};
下边是没有返回值的方法:
void DFS(vector<Employee*> employees,Employee* employee, int& sum) {
if (employee->subordinates.size() == 0) {
sum += employee->importance;
return;
}
sum += employee->importance;
for (int i = 0; i < employee->subordinates.size(); i++) {
for (Employee* e : employees) {
if (e->id == employee->subordinates[i]) {
DFS(employees, e, sum);
}
}
}
}
没有啥区别。。。
然而有一个困惑:
如果没有将员工信息收录到map里,计算的时候,就要根据subordinate的ID遍历数组找到那个员工,我做了两个不同的函数,一个是用了map,一个是没有用map,发现没有用map的函数执行时间反而少。。。 求指导?~
/*
// Employee info
class Employee {
public:
// It's the unique ID of each node.
// unique id of this employee
int id;
// the importance value of this employee
int importance;
// the id of direct subordinates
vector<int> subordinates;
};
*/
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
/*
int importance = 0;
unordered_map<int, Employee*> employees_map;
for (Employee* e: employees) {
employees_map[e->id] = e;
}
DFS(employees_map, employees_map[id], importance);
return importance;
*/
int importance = 0;
for (Employee* e : employees) {
if (e->id == id) {
DFS(employees, e, importance);
}
}
return importance;
}
//用了map的方法在LeetCode上运行46ms
void DFS(unordered_map<int, Employee*> employees_map,Employee* employee, int& sum) {
if (employee->subordinates.size() == 0) {
sum += employee->importance;
return;
}
sum += employee->importance;
for (int i = 0; i < employee->subordinates.size(); i++) {
DFS(employees_map, employees_map[employee->subordinates[i]], sum);
}
}
//没有用map的方法在LeetCode上运行19ms
void DFS(vector<Employee*> employees,Employee* employee, int& sum) {
if (employee->subordinates.size() == 0) {
sum += employee->importance;
return;
}
sum += employee->importance;
for (int i = 0; i < employee->subordinates.size(); i++) {
for (Employee* e : employees) {
if (e->id == employee->subordinates[i]) {
DFS(employees, e, sum);
}
}
}
}
};
这个问题很奇怪,求大神解答。。。