首页 > 其他分享 >leetcode690-员工的重要性

leetcode690-员工的重要性

时间:2022-08-16 20:45:50浏览次数:89  
标签:map total int list leetcode690 重要性 Employee 员工 id

员工的重要性

  • dfs
class Solution {
    Map<Integer, Employee> map = new HashMap<>();
    public int getImportance(List<Employee> employees, int id) {
        for(Employee e : employees)
            map.put(e.id, e);
        return dfs(id);
    }
    public int dfs(int id){
        Employee e = map.get(id);
        int total = e.importance;
        for(int val : e.subordinates)
            total += dfs(val);
        return total;
    }
}
  • bfs
class Solution {
    public int getImportance(List<Employee> employees, int id) {
        List<Employee> list = new ArrayList<>();
        Map<Integer, Employee> map = new HashMap<>();
        for(Employee e : employees)
            map.put(e.id, e);
        list.add(map.get(id));
        int start = 0, total = 0;
        while(start < list.size()){
            Employee e = list.get(start);
            total += e.importance;
            for(int val : e.subordinates)
                list.add(map.get(val));
            start++;
        }
        return total;
    }
}

标签:map,total,int,list,leetcode690,重要性,Employee,员工,id
From: https://www.cnblogs.com/xzh-yyds/p/16592908.html

相关文章