Skip to content

Instantly share code, notes, and snippets.

@balazsnemeth
Created February 8, 2017 22:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save balazsnemeth/3fbadbbdf7a5c27d8073463f5bed09ae to your computer and use it in GitHub Desktop.
Save balazsnemeth/3fbadbbdf7a5c27d8073463f5bed09ae to your computer and use it in GitHub Desktop.
Solution for MinAbsSum (Dynamic Programming)
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
// Not so optimal...
// Lesson: https://codility.com/programmers/lessons/17-dynamic_programming/min_abs_sum/
function solution(A) {
if (!A.length) {
return 0;
}
if (A.length === 1) {
return A[0];
}
var st = new Set()
for (a of A) {
if (st.size === 0) {
st.add(a);
}
else {
let stnew = new Set()
for (let s of st) {
stnew.add(Math.abs(s - a));
stnew.add(Math.abs(s + a));
}
st = stnew;
}
//console.log("set: ",st);
}
let absst = Array.from(st).map(se => Math.abs(se));
return Math.min.apply(null, absst);
}
@vsaravanan
Copy link

not working

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment