xxxxxxxxxx
39
const minblocksize = 3;
const length = 50;
let solutions = new Array(length + 1).fill(-1);
function setup() {
const solution = getLength(length,minblocksize);
console.log(`The solution is ${solution}`);
}
function getLength(space,minblocksize){
// input: current space and minblock size
console.log(`space = ${space}`)
// reached the end
if (space == 0){
return 1;
}
// potential optimization is to save results and not double compute
if (solutions[space] != -1){
return solutions[space];
}
// leave the next cell black
let result = getLength(space - 1,minblocksize);
// otherwise turn it red
for(let block = minblocksize; block <= space; block++){
// insert the block
let next = space - block;
if (next > 0) {
result += getLength(next-1, minblocksize);
}
}
solutions[space] = result;
console.log(`Setting solutions[${space}] = ${result}`);
return result;
}