【LeetCode690】Employee Importance

/*
// 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的索引。其实后两种时间差不多。

更新:

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.

Memory Usage: 15 MB, less than 75.00% of C++ online submissions for Employee Importance.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值