Created
August 29, 2015 19:53
-
-
Save axyz/360835af03513b0b7a86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
function greedyLinearPartition(seq, k) { | |
return seq | |
.sort((a, b) => b - a) | |
.reduce((res, el) => { | |
res[smallerArrayIndex(res)].push(el); | |
return res; | |
// waiting for more elegant solutions (Array.fill) to work correctly | |
}, new Array(k).join().split(',').map(i => [])); | |
} | |
function sum(arr) { | |
return arr.reduce((sum, el) => sum + el, 0); | |
} | |
function smallerArrayIndex(list) { | |
return list.reduce((i, array, index) => sum(array) < sum(list[i]) ? index : i, 0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment