Open this example in the interactive library
Source code
const tasks = [12, 8, 17, 7, 4],
blocks = [];
for (const n of tasks) {
const last = blocks.at(-1);
if (last && last.reduce((a, b) => a + b, 0) + n <= 25) last.push(n);
else blocks.push([n]);
}
console.log(blocks);
Sample output
[ [ 12, 8 ], [ 17, 7 ], [ 4 ] ]
