public class Solution { private HashMap employeeMap = new HashMap<>();
public int getImportance(List employees, int id) { // Step 1: Build the map from employee ID to employee object for (Employee e : employees) { employeeMap.put(e.id, e); } // Step 2: Calculate the total importance using recursion return sum(id); }
private int sum(int id) { Employee e = employeeMap.get(id); int totalImportance = e.importance; for (int subordinateId : e.subordinates) { totalImportance += sum(subordinateId); } return totalImportance; } } ```