Number of Ways to Build Sturdy Brick Wall - Problem
You're tasked with building a sturdy brick wall with specific dimensions and constraints. Given a wall of height rows and width units, you must construct it using bricks from an array where each brick has height 1 and width bricks[i].
Key Requirements:
- Each row must be exactly
widthunits long - You have an infinite supply of each brick type
- Bricks cannot be rotated
- Sturdy constraint: Adjacent rows cannot have joints at the same position (except at the wall ends)
Return the number of ways to build such a wall, modulo 109 + 7.
Example: With height=2, width=3, bricks=[1,2], you could have row 1 as [2,1] and row 2 as [1,2], since their internal joints don't align.
Input & Output
example_1.py โ Basic Case
$
Input:
height = 2, width = 3, bricks = [1, 2]
โบ
Output:
2
๐ก Note:
Two valid ways: Row1=[2,1] Row2=[1,2], and Row1=[1,2] Row2=[2,1]. In both cases, internal joints don't align.
example_2.py โ Single Row
$
Input:
height = 1, width = 4, bricks = [1, 2]
โบ
Output:
5
๐ก Note:
One row can be filled as: [1,1,1,1], [1,1,2], [1,2,1], [2,1,1], [2,2]. All are valid since there's no adjacent row to conflict with.
example_3.py โ No Valid Arrangement
$
Input:
height = 2, width = 3, bricks = [2]
โบ
Output:
0
๐ก Note:
Width 3 cannot be filled using only bricks of size 2 (3 is odd, but we need even sum). No valid arrangements exist.
Visualization
Tap to expand
Understanding the Visualization
1
Generate Row Patterns
Find all possible ways to fill one row with given bricks, recording internal joint positions
2
Pattern Compatibility
Create a matrix showing which row patterns can be placed adjacent to each other
3
Dynamic Programming
Use DP to count ways to build the wall, ensuring each row uses only compatible patterns
4
Sum All Possibilities
The final answer is the sum of all valid ways to complete the wall
Key Takeaway
๐ฏ Key Insight: The problem becomes manageable when we realize we only need to track joint positions (using bitmasks) rather than actual brick arrangements, and use dynamic programming to efficiently count valid wall configurations.
Time & Space Complexity
Time Complexity
O(P^2 * H)
Where P is the number of unique row patterns and H is height. We check compatibility between all pattern pairs once, then DP through H rows
โ Linear Growth
Space Complexity
O(P * H)
DP table storing ways to reach each pattern at each row level
โ Linear Space
Constraints
- 1 โค height โค 200
- 1 โค width โค 200
- 1 โค bricks.length โค 10
- 1 โค bricks[i] โค width
- All bricks[i] are unique
- Answer fits in 32-bit integer after modulo 109 + 7
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code