Created
February 8, 2017 22:15
-
-
Save balazsnemeth/3fbadbbdf7a5c27d8073463f5bed09ae to your computer and use it in GitHub Desktop.
Solution for MinAbsSum (Dynamic Programming)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
not working