From d906b86371338380e6b1c337f265338f9ef555bd Mon Sep 17 00:00:00 2001 From: aucker Date: Mon, 26 Aug 2024 08:59:39 +0800 Subject: [PATCH] Aug26: get importance DFS [M] DFS revisit M, traverse --- daily/Aug26.cc | 44 +++++++++++++++++++ ...mber-of-ways-to-divide-a-long-corridor.cpp | 3 +- 2 files changed, 45 insertions(+), 2 deletions(-) create mode 100644 daily/Aug26.cc diff --git a/daily/Aug26.cc b/daily/Aug26.cc new file mode 100644 index 0000000..c077370 --- /dev/null +++ b/daily/Aug26.cc @@ -0,0 +1,44 @@ +#include +#include +using namespace std; + +class Employee { + public: + int id; + int importance; + vector subordinates; +}; + +class Solution { + map m; + + public: + /** + * @brief Get the Importance object [M] DFS + * Time: O(N), Space: O(N) + * + * @param employees + * @param id + * @return int + */ + int getImportance(vector 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; + } +}; diff --git a/daily/cpp/Nov-28-Number-of-ways-to-divide-a-long-corridor.cpp b/daily/cpp/Nov-28-Number-of-ways-to-divide-a-long-corridor.cpp index 92f16b5..290ca29 100644 --- a/daily/cpp/Nov-28-Number-of-ways-to-divide-a-long-corridor.cpp +++ b/daily/cpp/Nov-28-Number-of-ways-to-divide-a-long-corridor.cpp @@ -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.