[Leetcode]690: Employee Importance

Link : https://leetcode.com/problems/employee-importance/

Problem :
You have a data structure of employee information, which includes the employee’s unique id, their importance value, and their direct subordinates’ id.

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 subordinates of the ith employee.

Given an integer id that represents the ID of an employee, return the total importance value of this employee and all their subordinates.

給一個id(整數),加總所有importance value。

Thoughts :
1. 先把整個employees的 list拆開成 importance跟subordinates。
2. 分別存入不同的dictionary。
3. 用遞迴來找,先記錄 id 的 importance 為 res,如果這個 employee 有subordinates 就把它加上 res,直到結束。

Solution :

time complexity = O(n), space complexity = O(n)
Note : n is number of employees



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store