LeetCode in Kotlin

690. Employee Importance

Medium

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:

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:

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:

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.

Constraints:

Solution

import com_github_leetcode.Employee

/*
 *	// Definition for Employee.
 *	class Employee {
 *		var id:Int = 0
 *		var importance:Int = 0
 *		var subordinates:List<Int> = listOf()
 *	}
 */
class Solution {
    fun getImportance(employees: List<Employee?>, id: Int): Int {
        val map: MutableMap<Int, Employee> = HashMap()
        for (emp in employees) {
            map[emp!!.id] = emp
        }
        return calculateImportance(id, map)
    }

    private fun calculateImportance(id: Int, map: Map<Int, Employee>): Int {
        val employee = map[id]
        var sum = employee!!.importance
        for (sub in employee.subordinates) {
            sum += calculateImportance(sub, map)
        }
        return sum
    }
}