Employee Importance


  • 0

    Click here to see the full article post


  • 0
    S

    appreciate it!


  • 0
    R

    My Java Solution

     public int getImportance(List<Employee> employees, int id) {
            int sum = 0;
            Employee target = null;
            for(Employee e : employees){
                if(e.id == id) target = e;
            }
            for(int i = 0; i < target.subordinates.size(); ++i){
                sum += getImportance(employees, target.subordinates.get(i));
            }
            return target.importance + sum;
        }
    

  • 0

    Here is a solution without a global variable.

    public int getImportance(List<Employee> employees, int id) {
        final Map<Integer, Employee> map = new HashMap<>();
        for (Employee employee : employees) {
            map.put(employee.id, employee);
        } 
        
        return getImportance(map, id);
    }
    
    private int getImportance(Map<Integer, Employee> map, int id) {
        final Employee employee = map.get(id);
        int importance = employee.importance;
        for (Integer number : employee.subordinates) {
            importance += getImportance(map, number);
        }
        
        return importance;
    }

  • 0

    My Python solution.

    class Solution(object):
        def getImportance(self, employees, id):
            """
            :type employees: Employee
            :type id: int
            :rtype: int
            """ 
            emap = {e.id: e for e in employees}
            employee = emap[id]
            total_importance = employee.importance    
            
            for id in employee.subordinates:
                total_importance += self.getImportance(employees, id)
                
            return total_importance
    

  • 0
    H

    Python Solution
    class Solution(object):
    def getImportance(self, employees, id):
    """
    :type employees: Employee
    :type id: int
    :rtype: int
    """
    dic = {employee.id: employee for employee in employees}
    return dic[id].importance + sum([self.getImportance(employees, subid) for subid in dic[id].subordinates])


  • 0
    J

    Java Solution using Streams API

    import java.util.List;
    import java.util.Map;
    import java.util.function.Function;
    import java.util.stream.Collectors;
    
    class Solution 
    {
        public int getImportance(List<Employee> employees, int id)
        {
            return _getImportance(employees.stream().collect(Collectors.toMap((Employee e) -> e.id , Function.identity())), id);
        }
    
        private int _getImportance(Map<Integer, Employee> employeeById, int id)
        {
            return employeeById.get(id).importance + employeeById.get(id).subordinates.stream().mapToInt(subId -> _getImportance(employeeById, subId)).sum();
        }
    }
    

  • 0
    A

    Java iterative solution

    class Solution {
        public int getImportance(List<Employee> employees, int id) {
            int result = 0;
            Map<Integer, Employee> m = new HashMap<>();
            Queue<Employee> q = new LinkedList<>();
            
            for (int i=0; i<employees.size(); i++){
                m.put(employees.get(i).id, employees.get(i));
            }
            q.add(m.get(id));
            
            while(!q.isEmpty()){
                Employee e = q.poll();
                for(int i=0; i<e.subordinates.size();i++){
                    q.add(m.get(e.subordinates.get(i)));
                }
                result += e.importance;
            }
            
            return result;
        }
    }
    

  • 0
    J

    class Solution {
    private int getValue(Map<Integer, Employee>m ,Employee employee, int value) {
    value += employee.importance;
    for (Integer sudId : employee.subordinates) {
    value = getValue(m, m.get(sudId), value);
    }
    return value;
    }

    public int getImportance(List<Employee> employees, int id) {
        Map<Integer, Employee> m = employees.stream().collect(Collectors.toMap(e -> e.id, Function.identity()));
        Employee employee = m.get(id);
        return getValue(m, employee, 0);
                
    
    }
    

    }


Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.