class Solution {
Map<Integer, Employee> map = new HashMap<Integer, Employee>();
public int getImportance(List<Employee> employees, int id) {
for (Employee employee : employees) {
map.put(employee.id, employee);
}
return dfs(id);
}
public int dfs(int id) {
Employee employee = map.get(id);
int total = employee.importance;
List<Integer> subordinates = employee.subordinates;
for (int subId : subordinates) {
total += dfs(subId);
}
return total;
}
}
Leetcode_690_员工的重要性_dfs
最新推荐文章于 2025-12-10 10:55:25 发布
本文介绍了一种通过深度优先搜索(DFS)算法计算组织结构中员工总重要性的方法。该算法首先将所有员工信息存储在哈希表中,然后递归地遍历每个员工及其下属,累加他们的个人重要性值。

641

被折叠的 条评论
为什么被折叠?



