-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
DFS revisit M, traverse
- Loading branch information
Showing
2 changed files
with
45 additions
and
2 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
#include <map> | ||
#include <vector> | ||
using namespace std; | ||
|
||
class Employee { | ||
public: | ||
int id; | ||
int importance; | ||
vector<int> subordinates; | ||
}; | ||
|
||
class Solution { | ||
map<int, Employee*> m; | ||
|
||
public: | ||
/** | ||
* @brief Get the Importance object [M] DFS | ||
* Time: O(N), Space: O(N) | ||
* | ||
* @param employees | ||
* @param id | ||
* @return int | ||
*/ | ||
int getImportance(vector<Employee*> employees, int id) { | ||
int len = employees.size(); | ||
for (int i = 0; i < len; i++) { | ||
m.insert({employees[i]->id, employees[i]}); | ||
} | ||
|
||
return getValue(id); | ||
} | ||
|
||
int getValue(int id) { | ||
Employee* master = m[id]; | ||
int ans = master->importance; | ||
for (int& oid : master->subordinates) { | ||
Employee* other = m[oid]; | ||
ans += other->importance; | ||
for (int& sub : other->subordinates) ans += getValue(sub); | ||
} | ||
|
||
return ans; | ||
} | ||
}; |
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