All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0690_employee_importance.Solution Maven / Gradle / Ivy

There is a newer version: 1.37
Show newest version
package g0601_0700.s0690_employee_importance;

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search

/*
// Definition for Employee.
class Employee {
    public int id;
    public int importance;
    public List subordinates;
};
*/

import com_github_leetcode.Employee;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Solution {
    public int getImportance(List employees, int id) {
        Map map = new HashMap<>();
        for (Employee emp : employees) {
            map.put(emp.id, emp);
        }
        return calculateImportance(id, map);
    }

    private int calculateImportance(int id, Map map) {
        Employee employee = map.get(id);
        int sum = employee.importance;
        for (int sub : employee.subordinates) {
            sum += calculateImportance(sub, map);
        }
        return sum;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy