Here, We see Employee Importance LeetCode Solution. This Leetcode problem is done in many programming languages like C++, Java, JavaScript, Python, etc. with different approaches.
List of all LeetCode Solution
![Employee Importance LeetCode Solution](https://i0.wp.com/totheinnovation.com/wp-content/uploads/2024/02/LeetCode-Problem-Solution.png?resize=200%2C200&ssl=1)
Employee Importance LeetCode Solution
Table of Contents
Problem Statement
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 1:
![emp1 tree](https://i0.wp.com/assets.leetcode.com/uploads/2021/05/31/emp1-tree.jpg?w=1400&ssl=1)
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.
Example 2:
![emp2 tree](https://i0.wp.com/assets.leetcode.com/uploads/2021/05/31/emp2-tree.jpg?w=1400&ssl=1)
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.
Employee Importance LeetCode Solution C++
class Solution {
public:
int getImportance(vector<Employee*> employees, int id) {
unordered_map<int, Employee*>m;
for(auto x: employees) m[x->id] = x;
int sum = 0;
deque<Employee*>q;
q.push_back(m[id]);
while(!q.empty()){
auto p = q.front();
q.pop_front();
for(auto x: p->subordinates) q.push_back(m[x]);
sum += p->importance;
}
return sum;
}
};
Code language: C++ (cpp)
Employee Importance LeetCode Solution Java
class Solution {
public int getImportance(List<Employee> employees, int id) {
Map<Integer, Employee> inputMap = new HashMap<>();
for(Employee e : employees) {
inputMap.put(e.id, e);
}
return helper(inputMap, id);
}
private static int helper(Map<Integer, Employee> inputMap, int id) {
int imp = inputMap.get(id).importance;
for(int subId : inputMap.get(id).subordinates) {
imp += helper(inputMap, subId);
}
return imp;
}
}
Code language: Java (java)
Employee Importance Solution JavaScript
var GetImportance = function(employees, id) {
let employeeMap = new Map();
for (employee of employees) {
employeeMap.set(employee.id, {importance : employee.importance, sub : employee.subordinates})
}
let totalImportance = 0;
let queue = [id];
while(queue.length > 0) {
let currentEmployee = employeeMap.get(queue.shift());
totalImportance += currentEmployee.importance;
queue.push(...currentEmployee.sub)
}
return totalImportance
};
Code language: JavaScript (javascript)
Employee Importance Solution Python
class Solution(object):
def getImportance(self, employees, id):
def dfs(emp):
imp = emps[emp].importance
for s in emps[emp].subordinates:
imp += dfs(s)
return imp
emps= {emp.id: emp for emp in employees}
return dfs(id)
Code language: Python (python)