- Lego Blocks
- Discussions

# Lego Blocks

# Lego Blocks

+ 0 comments I don't understan why for n = 3, m = 2 the solution is 7, the possible solutions are without repeats, with my code in javascript:

function legoBlocks(n, m) { const combinations = summa([], m); return buildFigure(combinations, [], n) } function buildFigure(combinations, figure, n) { if (figure.length < n) { return combinations.reduce((prev, element) => { if (figure.length > 0 && figure[figure.length - 1].length > 1 && element.length > 1) { for(let j = 0; j < figure[figure.length - 1].length; j++) { if (Array.from(figure[figure.length - 1])[j] === Array.from(element)[j]) { return prev } } } return prev + buildFigure(combinations, figure.concat(element), n) }, 0) } else { //figure.forEach(row => console.log(row)) return 1; } } function summa(row, m) { const tempSumma = row.reduce((prev, block) => prev + block, 0); if (tempSumma < m) { //console.log(`Is little ${row} - ${tempSumma}`) return [1, 2, 3, 4].flatMap(newBlock => { if (tempSumma + newBlock <= m) { return summa(row.concat([newBlock]), m) } else return '' }).filter(row => row.length > 0) } else if (tempSumma === m) { //console.log(`${row} - ${m}`) return row.join(' ') } else { return '' } }

1 1 2 1 1 1 1 2 2 2 1 1 2 2 2 1 1 2 2 2

+ 0 comments Just read editorial. I need to study more math.

+ 0 comments **here is problem solution in java python c++ and c programming**- https://programs.programmingoneonone.com/2021/07/hackerrank-lego-blocks-problem-solution.html

+ 0 comments There's a very unintuitive bit of the question which is that only COMPLETE vertical breaks should be avoided (i.e. that span the whole height). This is completely different to how you would intuitively build a wall in real life; you would not want any vertical breaks larger than height=1. There should at least be an example to make this clear as I don't think there's a neat solution for the more intuitively posed version of this question.

+ 1 comment Isn`t there a single O(1) math solution for this? I could only find a formula for a width (m) < 5, which would be: ((2^n)-1)^(m-1) (no matter what the height (n). But for m>=5 i couldnt find any formula.

Sort 10 Discussions, By:

Please Login in order to post a comment