Skip to content

Commit

Permalink
Aug26: get importance DFS [M]
Browse files Browse the repository at this point in the history
DFS revisit M, traverse
  • Loading branch information
aucker committed Aug 26, 2024
1 parent 3e3c00b commit d906b86
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
44 changes: 44 additions & 0 deletions daily/Aug26.cc
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;
}
};
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ class Solution {
if (corridor[i] == 'S') {
chairs++;

while (++i < corridor.size() && corridor[i] != 'S')
;
while (++i < corridor.size() && corridor[i] != 'S');
// the while increment i and check each character in the corridor
// string. the loop continues until the end of the string or until the
// next chair.
Expand Down

0 comments on commit d906b86

Please sign in to comment.