Employee Importance in Python

The employee importance problem calculates the total importance value of an employee and all their subordinates. Each employee has three attributes: a unique ID, importance value, and list of direct subordinate IDs.

Problem Understanding

Given a company's employee data structure, we need to find the cumulative importance of a specific employee including all subordinates in their hierarchy. For example, if employee 1 leads employees 2 and 3, the total importance is the sum of all three employees' individual importance values.

Algorithm Steps

We'll solve this using a breadth-first search approach ?

  • Create dictionaries to map employee IDs to their importance values and subordinates
  • Start with the target employee and add their importance to the result
  • Use a queue to process all subordinates level by level
  • For each subordinate, add their importance and queue their subordinates
  • Continue until all employees in the hierarchy are processed

Implementation

class Solution:
    def getImportance(self, employees, emp_id):
        # Create mappings for importance values and subordinates
        importance = {}
        subordinates = {}
        
        for emp in employees:
            importance[emp[0]] = emp[1]
            subordinates[emp[0]] = emp[2]
        
        # Start with the target employee's importance
        total_importance = importance[emp_id]
        
        # Process all subordinates using BFS
        queue = subordinates[emp_id][:]  # Copy to avoid modifying original
        
        while queue:
            current_emp = queue.pop(0)  # Remove from front (BFS)
            total_importance += importance[current_emp]
            
            # Add this employee's subordinates to queue
            if subordinates[current_emp]:
                queue.extend(subordinates[current_emp])
        
        return total_importance

# Test the solution
solution = Solution()
employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]
result = solution.getImportance(employees, 1)
print(f"Total importance: {result}")
Total importance: 11

Step-by-Step Execution

Let's trace through the example with employees [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]] and target ID 1 ?

# Step-by-step visualization
employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]
emp_id = 1

# Create mappings
importance = {1: 5, 2: 3, 3: 3}
subordinates = {1: [2, 3], 2: [], 3: []}

print(f"Starting with employee {emp_id}, importance: {importance[emp_id]}")
total = importance[emp_id]  # 5

queue = subordinates[emp_id][:]  # [2, 3]
print(f"Queue: {queue}")

step = 1
while queue:
    current = queue.pop(0)
    total += importance[current]
    print(f"Step {step}: Process employee {current}, add {importance[current]}, total: {total}")
    
    if subordinates[current]:
        queue.extend(subordinates[current])
        print(f"  Added subordinates: {subordinates[current]}")
    
    step += 1

print(f"Final total importance: {total}")
Starting with employee 1, importance: 5
Queue: [2, 3]
Step 1: Process employee 2, add 3, total: 8
Step 2: Process employee 3, add 3, total: 11
Final total importance: 11

Alternative Recursive Solution

We can also solve this problem using recursion for a more intuitive approach ?

class RecursiveSolution:
    def getImportance(self, employees, emp_id):
        # Create mapping for quick lookup
        emp_map = {emp[0]: emp for emp in employees}
        
        def dfs(employee_id):
            emp = emp_map[employee_id]
            # Add current employee's importance
            total = emp[1]
            
            # Recursively add subordinates' importance
            for subordinate_id in emp[2]:
                total += dfs(subordinate_id)
            
            return total
        
        return dfs(emp_id)

# Test recursive solution
recursive_solution = RecursiveSolution()
employees = [[1, 5, [2, 3]], [2, 3, []], [3, 3, []]]
result = recursive_solution.getImportance(employees, 1)
print(f"Recursive result: {result}")
Recursive result: 11

Time and Space Complexity

Approach Time Complexity Space Complexity Notes
BFS (Iterative) O(N) O(N) Queue space for subordinates
DFS (Recursive) O(N) O(H) H is hierarchy depth

Conclusion

Both BFS and DFS approaches effectively calculate employee importance by traversing the organizational hierarchy. The iterative BFS solution uses explicit queue management, while the recursive DFS leverages the call stack for a cleaner implementation.

Updated on: 2026-03-25T08:46:46+05:30

321 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements