blob: f0599b7a7e0f7fcaa0e943b3f8a7d074e5c9af0d (
plain)
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
49
50
51
52
|
#! /usr/bin/node
"use strict"
function combinations(arr, k) {
let c = [];
for (let i = 0; i < k; i++) {
c.push(i);
}
c.push(arr.length);
c.push(0);
let out = [];
while (true) {
let inner = [];
for (let i = k-1; i >= 0; i--) {
inner.push(arr[c[i]]);
}
out.push(inner);
let j = 0;
while (c[j] + 1 == c[j + 1]) {
c[j] = j;
j += 1;
}
if (j >= k) {
break;
}
c[j] += 1;
}
return out;
}
function grouphero(nums0) {
let nums = [...nums0];
nums.sort(function(a,b) {
return b-a;
});
let sum = 0;
for (let l = 1; l <= nums.length; l++) {
for (let c of combinations(nums, l)) {
const h = c[c.length - 1];
sum += h * h * c[0];
}
}
return sum;
}
if (grouphero([2, 1, 4]) == 141) {
process.stdout.write("Pass");
} else {
process.stdout.write("FAIL");
}
process.stdout.write("\n");
|