Wednesday, November 30, 2022

[Uber][LeetCode] Employee Importance

Problem: 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:

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.

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.


Approach: If we look closely it is kind of a tree problem. We are given the node of the tree and we need to get the sum of all the values of the nodes of the subtree whose root is given input node. 

We can build a tree using the given info and then we can simply apply DFS / preorder traversal to get the sum of the importance of all the nodes of the subtree.


Implementation in C#:

/*

// Definition for Employee.

class Employee {

    public int id;

    public int importance;

    public IList<int> subordinates;

}

*/


public class EmployeeValue

{

    public int Importance { get; set; }

    public IList<int> Children { get; set; }

    

    public EmployeeValue(int importance, IList<int> children)

    {

        this.Importance = importance;

        this.Children = children;

    }

}


class Solution 

{

    public int GetImportance(IList<Employee> employees, int id) 

    {

        var employeeTree = this.BuildEmployeeTree(employees);

        return this.GetImportanceHelper(employeeTree, id);

    }

    

    private int GetImportanceHelper(Dictionary<int, EmployeeValue> employeeTree, int currId)

    {

        int totalImportance = employeeTree[currId].Importance;

        foreach (int id in employeeTree[currId].Children)

        {

            totalImportance += this.GetImportanceHelper(employeeTree, id);

        }

        return totalImportance;

    }

    

    private Dictionary<int, EmployeeValue> BuildEmployeeTree(IList<Employee> employees)

    {

        var tree = new Dictionary<int, EmployeeValue>();

        foreach(var employee in employees)

        {

            tree[employee.id] = new EmployeeValue(employee.importance, employee.subordinates);

        }

        return tree;

    }

}


Complexity: O(n) where n is the length of the employees list.

No comments:

Post a Comment