-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMinAbsSum.js
More file actions
40 lines (35 loc) · 1 KB
/
Copy pathMinAbsSum.js
File metadata and controls
40 lines (35 loc) · 1 KB
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
//Score: 100%
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(A) {
// write your code in JavaScript (Node.js 8.9.4)
var orderedA = new Array(101).fill(0);
for(var i = 0; i < A.length; i++){
orderedA[Math.abs(A[i])] ++;
}
var values = new Set();
values.add(0);
var newvalue = 0;
for(var i = 100; i > -1; i--){
while(orderedA[i]){
var tempValues = new Set();
for(let j of values){
newvalue = j+i;
if(newvalue<=1000){
tempValues.add(newvalue);
}
newvalue = Math.abs(j-i);
if(newvalue<=1000){
tempValues.add(newvalue);
}
}
orderedA[i] --;
values = tempValues;
}
}
var min = +Infinity;
for(let j of values){
min = Math.min(min, Math.abs(j));
}
return min;
}