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

g0601_0700.s0690_employee_importance.Solution Maven / Gradle / Ivy

The newest version!
package g0601_0700.s0690_employee_importance;

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search
// #2022_03_22_Time_7_ms_(77.84%)_Space_58.8_MB_(21.60%)

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

/*
// Definition for Employee.
class Employee {
    public int id;
    public int importance;
    public List subordinates;
};
*/
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