forked from doocs/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.ts
28 lines (27 loc) · 812 Bytes
/
Solution.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
/**
* Definition for Employee.
* class Employee {
* id: number
* importance: number
* subordinates: number[]
* constructor(id: number, importance: number, subordinates: number[]) {
* this.id = (id === undefined) ? 0 : id;
* this.importance = (importance === undefined) ? 0 : importance;
* this.subordinates = (subordinates === undefined) ? [] : subordinates;
* }
* }
*/
function getImportance(employees: Employee[], id: number): number {
const d = new Map<number, Employee>();
for (const e of employees) {
d.set(e.id, e);
}
const dfs = (i: number): number => {
let s = d.get(i)!.importance;
for (const j of d.get(i)!.subordinates) {
s += dfs(j);
}
return s;
};
return dfs(id);
}