-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.js
28 lines (26 loc) · 859 Bytes
/
solution.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
const filePath = require('path').join(__dirname, 'input');
const [n, arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function solution (n, arr) {
const getMaxOfSum = (n, depth, max) => {
if (depth === n) {
max = Math.max(max, arr.reduce((acc, cur, index) => {
if (index >= arr.length - 1) return acc;
acc += Math.abs(cur - arr[index + 1]);
return acc;
}, 0))
return max;
}
for (let i = depth; i < n; i ++) {
[arr[i], arr[depth]] = [arr[depth], arr[i]];
max = getMaxOfSum(n, depth + 1, max);
[arr[i], arr[depth]] = [arr[depth], arr[i]];
}
return max;
}
return getMaxOfSum(n, 0, 0);
}
console.log(solution(+n, arr.split(' ').map(a => +a)));