zoukankan      html  css  js  c++  java
  • LeetCode 690. Employee Importance (职员的重要值)

    You are given a data structure of employee information, which includes the employee's unique id, his importance value and his directsubordinates' id.

    For example, employee 1 is the leader of employee 2, and employee 2 is the leader of employee 3. They have importance value 15, 10 and 5, respectively. Then employee 1 has a data structure like [1, 15, [2]], and employee 2 has [2, 10, [3]], and employee 3 has [3, 5, []]. Note that although employee 3 is also a subordinate of employee 1, the relationship is not direct.

    Now given the employee information of a company, and an employee id, you need to return the total importance value of this employee and all his subordinates.

    Example 1:

    Input: [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]], 1
    Output: 11
    Explanation:
    Employee 1 has importance value 5, and he has two direct subordinates: employee 2 and employee 3. They both have importance value 3. So the total importance value of employee 1 is 5 + 3 + 3 = 11.
    

    Note:

    1. One employee has at most one direct leader and may have several subordinates.
    2. The maximum number of employees won't exceed 2000.

     题目标签:HashTable

      题目给了我们一个 employees list, 和 一个 id,让我们找到这个 id 的员工的手下所有员工的 importance 累加,包括他自己的。

      首先把 employees 存入 HashMap, id 为 key, Employee 为value。

      然后建立一个 dfs function:

        当员工的 subordinates 的 size  等于 0 的时候, 说明没有必要继续递归了,返回员工的重要值;

        如果 size 大于0,那么遍历 subordinates,把每一个 员工id 递归,累加重要值。

    Java Solution:

    Runtime beats 71.9% 

    完成日期:11/16/2017

    关键词:HashMap, DFS

    关键点:把employee 信息存入map,id 为 key,employee 为 value,便于dfs 直接调取 员工信息

     1 /*
     2 // Employee info
     3 class Employee {
     4     // It's the unique id of each node;
     5     // unique id of this employee
     6     public int id;
     7     // the importance value of this employee
     8     public int importance;
     9     // the id of direct subordinates
    10     public List<Integer> subordinates;
    11 };
    12 */
    13 class Solution 
    14 {
    15     public int getImportance(List<Employee> employees, int id) 
    16     {
    17         HashMap<Integer, Employee> map = new HashMap<>();
    18         
    19         for(Employee e: employees)
    20             map.put(e.id, e);
    21              
    22         return dfs(id, map);
    23     }
    24     
    25     private int dfs(int id, HashMap<Integer, Employee> map)
    26     {
    27         Employee e = map.get(id);
    28         
    29         if(e.subordinates.size() == 0)
    30             return e.importance;
    31         
    32         int imp = e.importance;
    33         
    34         for(int sub: e.subordinates)
    35             imp += dfs(sub, map);
    36     
    37         
    38         return imp;
    39     }
    40 }

    参考资料:N/A

    LeetCode 题目列表 - LeetCode Questions List

    题目来源:https://leetcode.com/

  • 相关阅读:
    常见设备标记长度查询
    word怎么在方框中打对号
    shell dict 操作
    词表数据转换
    GoLand tool tips
    mac使用技巧
    人生三大陷阱
    【js重学系列】执行上下文
    uniapp-ui库
    【js重学系列】instanceof
  • 原文地址:https://www.cnblogs.com/jimmycheng/p/7848558.html
Copyright © 2011-2022 走看看