Created
May 17, 2018 13:58
-
-
Save s4553711/6a698ce337b3e3a8ed022e77b57f696f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
// 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