There is a wall of size 4xN. We have infinite number of bricks of size 4x1 and 1x4. What is the total number of ways in which the bricks can be arranged on the wall so that a new configuration arises every time?
For N = 1, the brick can be laid in 1 format only. For N = 7, one of the ways in which we can lay the bricks is
There are 5 ways of arranging the bricks for N = 7
I solve this problem using dynamic programming:
static int computeBricks(int n)
{
if(n <= 3) { return 1; }
int[] table = new int[n+1];
table[0] = 1;
table[1] = 1;
table[2] = 1;
table[3] = 1;
for(int i = 4; i <= n; ++i)
{
table[i] = table[i-1] + table[i-4];
}
return table[n];
}
But it's just a combination: a guess + intuition. I don't understand this solution completely. Why table[i] = table[i-1] + table[i-4]?
Doesn't it look like the coin change problem?
The number of ways to change amount a using n kinds of coins equals:
the number of ways to change amount a using all but the first kind of coin, plus the number of ways to change amount a - d using all n kinds of coins, where d is the denomination of the first kind of coin.
But I also don't understand how we can use this idea to solve the original problem




coin change problemideas? That's how we can solvecoinsproblemf(N, coins, m - 1) + f(n - coins[m - 1], coins, m);How to transform this solution to compute the original problem about bricks? - user6611771f(i) = f(i-1) + f(i-4). That's simpler than the coin change problem because there are only two "coins". The multiple coin equivalent would beF(N, coins, m, i) = f(N, coins, m, i-1) + f(N - coins[i-1], coins, m, m): in other words, either don't use "this coin" (index i) or use "this coin" and restart the scan. - rici