LeetCode 690: Employee Importance Solution

Master LeetCode problem 690 (Employee Importance), a medium challenge, with our optimized solutions in Java, C++, and Python. Explore detailed explanations, test your code in our interactive editor, and prepare for coding interviews.

Problem Explanation

Explanation:

To solve this problem, we can use a depth-first search (DFS) algorithm to traverse the employee hierarchy starting from the given employee ID. We will keep track of the total importance value by summing up the importance values of the current employee and recursively calling the function for each of its direct subordinates.

Algorithm:

  1. Create a hashmap to store the employee objects indexed by their IDs for easy access.
  2. Define a recursive function getTotalImportance that takes an employee ID as input and returns the total importance value.
  3. In the function:
    • Get the employee object using the given ID.
    • Add the importance value of the current employee to the total importance.
    • Recursively call the function for each direct subordinate and add their importance values.
    • Return the total importance value.
  4. Call the function with the given employee ID to get the result.

Time Complexity:

  • The time complexity of this algorithm is O(N), where N is the total number of employees in the input list.

Space Complexity:

  • The space complexity is also O(N) due to the hashmap used to store employee objects.

:

Solution Code

import java.util.*;

class Employee {
    public int id;
    public int importance;
    public List<Integer> subordinates;
}

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

    private int getTotalImportance(Map<Integer, Employee> map, int id) {
        Employee employee = map.get(id);
        int totalImportance = employee.importance;
        for (int subId : employee.subordinates) {
            totalImportance += getTotalImportance(map, subId);
        }
        return totalImportance;
    }
}

Try It Yourself

Loading code editor...

Related LeetCode Problems

Frequently Asked Questions

How to solve LeetCode 690 (Employee Importance)?

This page provides optimized solutions for LeetCode problem 690 (Employee Importance) in Java, C++, and Python, along with a detailed explanation and an interactive code editor to test your code.

What is the time complexity of LeetCode 690 (Employee Importance)?

The time complexity for LeetCode 690 (Employee Importance) varies by solution. Check the detailed explanation section for specific complexities in Java, C++, and Python implementations.

Can I run code for LeetCode 690 on DevExCode?

Yes, DevExCode provides an interactive code editor where you can write, test, and run your code for LeetCode 690 in Java, C++, or Python.

Back to LeetCode Solutions