返回介绍

solution / 0600-0699 / 0690.Employee Importance / README_EN

发布于 2024-06-17 01:03:35 字数 4704 浏览 0 评论 0 收藏 0

690. Employee Importance

中文文档

Description

You have a data structure of employee information, including the employee's unique ID, importance value, and direct subordinates' IDs.

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 direct subordinates of the ith employee.

Given an integer id that represents an employee's ID, return _the total importance value of this employee and all their direct and indirect subordinates_.

 

Example 1:

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

Example 2:

Input: employees = [[1,2,[5]],[5,-3,[]]], id = 5
Output: -3
Explanation: Employee 5 has an importance value of -3 and has no direct subordinates.
Thus, the total importance value of employee 5 is -3.

 

Constraints:

  • 1 <= employees.length <= 2000
  • 1 <= employees[i].id <= 2000
  • All employees[i].id are unique.
  • -100 <= employees[i].importance <= 100
  • One employee has at most one direct leader and may have several subordinates.
  • The IDs in employees[i].subordinates are valid IDs.

Solutions

Solution 1

"""
# Definition for Employee.
class Employee:
  def __init__(self, id: int, importance: int, subordinates: List[int]):
    self.id = id
    self.importance = importance
    self.subordinates = subordinates
"""


class Solution:
  def getImportance(self, employees: List['Employee'], id: int) -> int:
    m = {emp.id: emp for emp in employees}

    def dfs(id: int) -> int:
      emp = m[id]
      s = emp.importance
      for sub in emp.subordinates:
        s += dfs(sub)
      return s

    return dfs(id)
/*
// Definition for Employee.
class Employee {
  public int id;
  public int importance;
  public List<Integer> subordinates;
};
*/

class Solution {

  private final Map<Integer, Employee> map = new HashMap<>();

  public int getImportance(List<Employee> employees, int id) {
    for (Employee employee : employees) {
      map.put(employee.id, employee);
    }
    return dfs(id);
  }

  private int dfs(int id) {
    Employee employee = map.get(id);
    int sum = employee.importance;
    for (Integer subordinate : employee.subordinates) {
      sum += dfs(subordinate);
    }
    return sum;
  }
}
/**
 * Definition for Employee.
 * function Employee(id, importance, subordinates) {
 *   this.id = id;
 *   this.importance = importance;
 *   this.subordinates = subordinates;
 * }
 */

/**
 * @param {Employee[]} employees
 * @param {number} id
 * @return {number}
 */
var GetImportance = function (employees, id) {
  const map = new Map();
  for (const employee of employees) {
    map.set(employee.id, employee);
  }
  const dfs = id => {
    const employee = map.get(id);
    let sum = employee.importance;
    for (const subId of employee.subordinates) {
      sum += dfs(subId);
    }
    return sum;
  };
  return dfs(id);
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文