Skip to content

Commit adbf8b3

Browse files
committed
Add solution #690
1 parent f8405e7 commit adbf8b3

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,7 @@
521521
687|[Longest Univalue Path](./0687-longest-univalue-path.js)|Medium|
522522
688|[Knight Probability in Chessboard](./0688-knight-probability-in-chessboard.js)|Medium|
523523
689|[Maximum Sum of 3 Non-Overlapping Subarrays](./0689-maximum-sum-of-3-non-overlapping-subarrays.js)|Hard|
524+
690|[Employee Importance](./0690-employee-importance.js)|Medium|
524525
693|[Binary Number with Alternating Bits](./0693-binary-number-with-alternating-bits.js)|Easy|
525526
695|[Max Area of Island](./0695-max-area-of-island.js)|Medium|
526527
696|[Count Binary Substrings](./0696-count-binary-substrings.js)|Easy|

solutions/0690-employee-importance.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* 690. Employee Importance
3+
* https://leetcode.com/problems/employee-importance/
4+
* Difficulty: Medium
5+
*
6+
* You have a data structure of employee information, including the employee's unique ID,
7+
* importance value, and direct subordinates' IDs.
8+
*
9+
* You are given an array of employees employees where:
10+
* - employees[i].id is the ID of the ith employee.
11+
* - employees[i].importance is the importance value of the ith employee.
12+
* - employees[i].subordinates is a list of the IDs of the direct subordinates of the ith employee.
13+
*
14+
* Given an integer id that represents an employee's ID, return the total importance value of this
15+
* employee and all their direct and indirect subordinates.
16+
*/
17+
18+
/**
19+
* Definition for Employee.
20+
* function Employee(id, importance, subordinates) {
21+
* this.id = id;
22+
* this.importance = importance;
23+
* this.subordinates = subordinates;
24+
* }
25+
*/
26+
27+
/**
28+
* @param {Employee[]} employees
29+
* @param {number} id
30+
* @return {number}
31+
*/
32+
var GetImportance = function(employees, id) {
33+
const map = new Map(employees.map(e => [e.id, e]));
34+
return dfs(id);
35+
36+
function dfs(id) {
37+
if (!map.has(id)) return 0;
38+
const e = map.get(id);
39+
return e.importance + e.subordinates.reduce((sum, id) => sum + dfs(id), 0);
40+
}
41+
};

0 commit comments

Comments
 (0)