Skip to content

Instantly share code, notes, and snippets.

@s4553711
Created May 17, 2018 13:58
Show Gist options
  • Save s4553711/6a698ce337b3e3a8ed022e77b57f696f to your computer and use it in GitHub Desktop.
Save s4553711/6a698ce337b3e3a8ed022e77b57f696f to your computer and use it in GitHub Desktop.
/*
// 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) {
unordered_map<int, Employee*> es;
for(auto e : employees) {
es.emplace(e->id, e);
}
return dfs(id, es);
}
int dfs(int id, unordered_map<int, Employee*>& es) {
const auto e = es.at(id);
int sum = e->importance;
for(int rid : e->subordinates) {
sum += dfs(rid, es);
}
return sum;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment