栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

LeetCode知识点总结 - 690

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

LeetCode知识点总结 - 690

LeetCode 690. Employee importance
考点难度
Hash TableEasy
题目

You have a data structure of employee information, which includes the employee’s unique id, their importance value, and their direct subordinates’ id.

You are given an array of employees employees where:

employees[i].id is the ID of the ith employee.
employees[i].importance is the importance value of the ith employee.
employees[i].subordinates is a list of the IDs of the subordinates of the ith employee.
Given an integer id that represents the ID of an employee, return the total importance value of this employee and all their subordinates.

思路

把每个employee的信息存储在一个HashMap里。新建一个helper function(depth first search),从给定的id开始,遍历该employee的所有subordinates,这里用到了recursion。有了helper function之后可以直接返回这个function的结果。

答案
public int getimportance(List employees, int id) {
        Map map = new HashMap<> ();
        for (Employee e: employees){
            map.put(e.id, e);
        }
        return dfs(id, map);
}
    
public int dfs(int id, Map m){
        int ans = m.get(id).importance;
        for(int newId: m.get(id).subordinates){
            ans += dfs(newId, m);
        }
        return ans;
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/285093.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号