All Downloads are FREE. Search and download functionalities are using the official Maven repository.

g0601_0700.s0690_employee_importance.Solution.kt Maven / Gradle / Ivy

There is a newer version: 1.32
Show newest version
package g0601_0700.s0690_employee_importance

// #Medium #Hash_Table #Depth_First_Search #Breadth_First_Search
// #2023_02_20_Time_255_ms_(100.00%)_Space_46.3_MB_(71.43%)

import com_github_leetcode.Employee

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

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




© 2015 - 2025 Weber Informatics LLC | Privacy Policy