- Leetcode 690: Employee importance
- Level: easy
- Link
My approach
- Iterate through nested list and store employee id and importance in a dictionary
- Perform dfs with starting id
- sum up the importance of an employee
- check if subordinate of the employee exists
- If exists, perform recursion on the subordinate employees
- Return the total sum of employee
class Solution:
def getImportance(self, employees: List['Employee'], id: int) -> int:
diction = {}
# store employee id and importance
for info in employees:
diction[info.id] = info.importance
res = 0
for employee in employees:
if employee.id == id:
res += employee.importance
if employee.subordinates:
for subs in employee.subordinates:
res += self.getImportance(employees, subs)
return (res)
'개발공부 > algorithm' 카테고리의 다른 글
[Leetcode][python] 257: binary tree paths - DFS (0) | 2021.04.27 |
---|---|
[Leetcode][python] 733: flood fill - bfs (0) | 2021.04.25 |
[백준][python] 4889 안정적인 문자열 - Greedy (0) | 2021.04.25 |
[백준][python] 1946 신입사원 - Greedy (0) | 2021.04.21 |
[프로그래머스][python] 전화번호 목록 (0) | 2021.04.17 |