/* // 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) { // if((employees[id]->subordinates).empty()) // return employees[id]->importance; // int sum = employees[id]->importance; // for(int i = 0; i < (employees[id]->subordinates).size(); ++i){ // sum += getImportance(employees, (employees[id]->subordinates)[i]); // } // return sum; // } // }; // class Solution { // public: // unordered_map<int, int> temp; // int getImportance(vector<Employee*> employees, int id) { // //we have to find out which one in the vector employees has the correct id. // for(int i =0; i < employees.size(); ++i){ // temp[employees[i]->id] = i; // } // return help_getImportance(employees, id); // } // int help_getImportance(vector<Employee*> employees, int id) { // Employee* p_employ = employees[temp[id]]; // if((p_employ->subordinates).empty()) // return p_employ->importance; // int sum = p_employ->importance; // for(int i = 0; i < (p_employ->subordinates).size(); ++i){ // sum += help_getImportance(employees, (p_employ->subordinates)[i]); // } // return sum; // } // }; class Solution { public: unordered_map<int, Employee*> temp; int getImportance(vector<Employee*> employees, int id) { //we have to find out which one in the vector employees has the correct id. for(int i =0; i < employees.size(); ++i){ temp[employees[i]->id] = employees[i]; } return help_getImportance(temp, id); } int help_getImportance(unordered_map<int, Employee*> temp, int id) { Employee* p_employ = temp[id]; // if((p_employ->subordinates).empty()) // return p_employ->importance; int sum = p_employ->importance; // for(int i = 0; i < (p_employ->subordinates).size(); ++i){ // sum += help_getImportance(temp, (p_employ->subordinates)[i]); // } for(int id_sub : (p_employ->subordinates)){ sum += help_getImportance(temp, id_sub); } return sum; } };
第一种方法是错的,理解题目错了,由于给定的id并非对应employees的索引,须要本身建哈希表,把id与该id对应的Employee*型指针造成映射(或者在employees中的索引),抛弃方法二中的employees的索引。其实后两种时间差很少。node
更新:less
class Solution { public: unordered_map<int, Employee*> temp; int getImportance(vector<Employee*> employees, int id) { //we have to find out which one in the vector employees has the correct id. for(Employee* e : employees){ temp[e->id] = e; } return help_getImportance(id); } int help_getImportance(int id) { Employee* p_employ = temp[id]; // if((p_employ->subordinates).empty()) // return p_employ->importance; int sum = p_employ->importance; // for(int i = 0; i < (p_employ->subordinates).size(); ++i){ // sum += help_getImportance(temp, (p_employ->subordinates)[i]); // } for(int id_sub : (p_employ->subordinates)){ sum += help_getImportance(id_sub); } return sum; } };
这里对help函数,再也不传入哈希表做为实参,从而大大提高了速度,也大大下降了堆栈溢出的风险。函数
Runtime: 32 ms, faster than 97.67% of C++ online submissions for Employee Importance.this
Memory Usage: 15 MB, less than 75.00% of C++ online submissions for Employee Importance.指针