/*// Definition for Employee.class Employee { public int id; public int importance; public List<Integer> subordinates;};*/classSolution{privatefinalMap<Integer,Employee>d=newHashMap<>();publicintgetImportance(List<Employee>employees,intid){for(vare:employees){d.put(e.id,e);}returndfs(id);}privateintdfs(inti){Employeee=d.get(i);ints=e.importance;for(intj:e.subordinates){s+=dfs(j);}returns;}}
/*// Definition for Employee.class Employee {public: int id; int importance; vector<int> subordinates;};*/classSolution{public:intgetImportance(vector<Employee*>employees,intid){unordered_map<int,Employee*>d;for(auto&e:employees){d[e->id]=e;}function<int(int)>dfs=[&](inti)->int{ints=d[i]->importance;for(intj:d[i]->subordinates){s+=dfs(j);}returns;};returndfs(id);}};
1 2 3 4 5 6 7 8 9101112131415161718192021222324
/** * Definition for Employee. * type Employee struct { * Id int * Importance int * Subordinates []int * } */funcgetImportance(employees[]*Employee,idint)int{d:=map[int]*Employee{}for_,e:=rangeemployees{d[e.Id]=e}vardfsfunc(int)intdfs=func(iint)int{s:=d[i].Importancefor_,j:=ranged[i].Subordinates{s+=dfs(j)}returns}returndfs(id)}