-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution4.js
48 lines (44 loc) · 1.17 KB
/
solution4.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
42
43
44
45
46
47
48
const filePath = require('path').join(__dirname, 'input');
const [n, ...arr] = require('fs')
.readFileSync(filePath)
.toString()
.trim()
.split('\n');
function solution (n, arr) {
const dp = new Array(2 ** n).fill(0);
const dfs = (bit, index, depth) => {
if (depth === n) return;
for (let i = index; i < n; i ++) {
const next = bit | 1 << i;
dp[next] = dp[bit];
for (let j = 0; j < n; j ++) {
if (bit & 1 << j) {
dp[next] += arr[i][j] + arr[j][i];
}
}
dfs(next, i + 1, depth + 1);
}
}
dfs(0, 0, 0);
let min = Infinity;
const len = 1 << n;
for (let i = 1; i < len; i ++) {
const diff = Math.abs(dp[i] - dp[len - i - 1]);
if (diff < min) {
min = diff;
}
}
return min;
}
console.log(solution(+n, arr.map(r => r.split(' ').map(Number))));
function run (input) {
const [n, ...arr] = input
.toString()
.trim()
.split('\n')
.map(r => r.trim())
return solution(+n, arr.map(r => r.split(' ').map(Number)))
}
module.exports = {
run
}