Skip to content

Instantly share code, notes, and snippets.

@axyz
Created August 29, 2015 19:53
Show Gist options
  • Save axyz/360835af03513b0b7a86 to your computer and use it in GitHub Desktop.
Save axyz/360835af03513b0b7a86 to your computer and use it in GitHub Desktop.
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