Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Program to count number of horizontal brick pattern can be made from set of bricks in Python
Suppose we have a list of numbers called bricks and two other values width and height. Each element in bricks[i] represents a brick whose length is bricks[i] units and width is 1 unit. We have to find the number of ways to lay the bricks such that we get full layout of bricks with the given width and height. We can reuse the bricks but can only be laid horizontally.
So, if the input is like bricks = [2, 1] width = 3 height = 2, then the output will be 9 because −
Algorithm
To solve this, we will follow these steps −
- w := a list of size same as width and insert 1 at first position, rest are 0
- for i in range 0 to width, do
- if w[i] is non-zero, then
- for each x in bricks, do
- if i + x <= width, then
- w[i + x] := w[i + x] + w[i]
- if i + x <= width, then
- for each x in bricks, do
- if w[i] is non-zero, then
- return w[width]^height
Example
Let us see the following implementation to get better understanding ?
def solve(bricks, width, height):
w = [1] + [0] * width
for i in range(width):
if w[i]:
for x in bricks:
if i + x <= width:
w[i + x] += w[i]
return w[width] ** height
bricks = [2, 1]
width = 3
height = 2
print(solve(bricks, width, height))
The output of the above code is ?
9
How It Works
The algorithm uses dynamic programming to count the ways to fill one row, then raises that count to the power of height:
- We create an array
wwherew[i]represents the number of ways to fill exactlyiunits - Initially
w[0] = 1(one way to fill 0 units: use no bricks) - For each position
i, if there arew[i]ways to reach it, we can place each brick type to extend further - Finally,
w[width]gives ways to fill one row, and we raise it to power of height for all rows
Step-by-Step Trace
For bricks = [2, 1], width = 3 ?
def solve_with_trace(bricks, width, height):
w = [1] + [0] * width
print(f"Initial: {w}")
for i in range(width):
if w[i]:
print(f"\nFrom position {i} (ways: {w[i]}):")
for x in bricks:
if i + x <= width:
w[i + x] += w[i]
print(f" Place brick {x}: w[{i + x}] += {w[i]} = {w[i + x]}")
print(f"Updated: {w}")
print(f"\nWays to fill one row: {w[width]}")
print(f"Ways to fill {height} rows: {w[width]}^{height} = {w[width] ** height}")
return w[width] ** height
bricks = [2, 1]
width = 3
height = 2
solve_with_trace(bricks, width, height)
Initial: [1, 0, 0, 0] From position 0 (ways: 1): Place brick 2: w[2] += 1 = 1 Place brick 1: w[1] += 1 = 1 Updated: [1, 1, 1, 0] From position 1 (ways: 1): Place brick 2: w[3] += 1 = 1 Place brick 1: w[2] += 1 = 2 Updated: [1, 1, 2, 1] From position 2 (ways: 2): Place brick 1: w[3] += 2 = 3 Updated: [1, 1, 2, 3] Ways to fill one row: 3 Ways to fill 2 rows: 3^2 = 9
Conclusion
This dynamic programming solution efficiently counts brick laying patterns by calculating ways to fill one row, then raising to the power of height. The time complexity is O(width × number of brick types) per row calculation.
