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.
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
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.
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code