DFS:690. Employee Importance
来源:互联网 发布:达芬奇 去闪烁 mac 编辑:程序博客网 时间:2024/05/21 21:03
这道题是说,每一个员工有三个属性:id,重要性importance,直系下属id[]。题目给出每个员工的信息,并且给出一个id,求出这个员工的所有下属。
/*// Employee infoclass 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*> m; for(auto e : employees) { m[e->id] = e; } int sum = 0; return dfs(m, id, sum); } int dfs(unordered_map<int, Employee*>& m, int id, int& sum) { sum += m[id]->importance; for(auto i : m[id]->subordinates) { dfs(m, i, sum); } return sum; }};
讨论区:
class Solution {public: int getImportance(vector<Employee*> employees, int id) { unordered_map<int, Employee*> map; for(const auto element : employees){ map[element->id] = element; } return help(map, id); } int help(unordered_map<int, Employee*>& map, const int id){ auto sum = map[id]->importance; for(const auto element : map[id]->subordinates){ sum += help(map, element); } return sum; }};
阅读全文
0 0
- DFS:690. Employee Importance
- 690. Employee Importance
- [LeetCode] 690. Employee Importance
- leetcode 690. Employee Importance
- [LeetCode] 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- leetcode-690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- 690. Employee Importance
- LeetCode 690.Employee Importance
- Leetcode:690. Employee Importance
- leetcode 690. Employee Importance 深度优先遍历DFS
- 完全背包
- 第六章、ReactNative预加载解决方案
- 历史文化名城
- NKOJ 3893 聪聪和可可(数学期望+递推+最短路)
- 在另一个进程中注入代码的方式20171013
- DFS:690. Employee Importance
- Java/Web调用Hadoop进行MapReduce
- 异或运算例题
- 剑指offer之二十一---复杂链表的复制
- [MIPS汇编语言]对于数的输入和输出
- redis追加持久化-aof(append only file)
- 设计模式 C++版:第二十三式 模板方法
- 成长之路20171013
- 231. Power of Two