-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1135.js
41 lines (35 loc) · 1.2 KB
/
1135.js
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
/*
자기 직속 부하들에게 전화를 거는 순서가 중요
본인의 직속 부하들 중
그 부하의 부하들에게 전파하는 데 가장 오래 걸리는 직원부터 전화해야 함
max(N)=50이므로 재귀를 이용한 dfs로 처리 가능
*/
const makeTree = (N, superiors) => {
const subordinates = Array.from(new Array(N), () => []);
superiors.forEach((superior, index) => {
if (index > 0) subordinates[superior].push(index);
});
return subordinates;
};
const computeTransferTime = (current, subordinates) => {
// base case
if (subordinates[current].length === 0) return 0;
// recursive step
let times = [];
subordinates[current].forEach((subordinate) => {
times.push(computeTransferTime(subordinate, subordinates));
});
times.sort((a, b) => b - a);
times = times.map((time, index) => time + index + 1);
return Math.max(...times);
};
// input
const INPUT_FILE = process.platform === 'linux' ? '/dev/stdin' : './input';
const [N, ...superiors] = require('fs').readFileSync(INPUT_FILE).toString().trim()
.split(/\s/)
.map(Number);
// process
const subordinates = makeTree(N, superiors);
const sol = computeTransferTime(0, subordinates);
// output
console.log(sol);