-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy path690-employee-importance.js
More file actions
41 lines (38 loc) · 967 Bytes
/
690-employee-importance.js
File metadata and controls
41 lines (38 loc) · 967 Bytes
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
29
30
31
32
33
34
35
36
37
38
39
40
41
/**
* Definition for Employee.
* function Employee(id, importance, subordinates) {
* this.id = id;
* this.importance = importance;
* this.subordinates = subordinates;
* }
*/
/**
* @param {Employee[]} employees
* @param {number} id
* @return {number}
*/
var GetImportance = function(employees, id) {
// approach: start with curr employee and hop to their subordinates
// and sub-subordinates until we reach an employee with no subordinates
// pre-processing: create an easy way to look up employees by their id
const h = {}
for(let i=0; i<employees.length; i++) {
const employee = employees[i]
h[employee.id] = employee
}
// BFS
const toExplore = []
const seen = {}
toExplore.push(id)
let res = 0
while(toExplore.length>0) {
curr = h[toExplore.shift()]
curr.subordinates.forEach(id => {
if(!seen[id]) {
toExplore.push(id)
}
})
res += curr.importance
}
return res
};