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 width units 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
Row 1: [2,2]12Joint at pos 2Row 1: [2,2]111Joints at pos 1,2โŒ Joints align - Invalid!โœ… No joint alignment - Valid!Key InsightUse bitmasks to represent joint positionsDP to count compatible combinations
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

n
2n
โœ“ Linear Growth
Space Complexity
O(P * H)

DP table storing ways to reach each pattern at each row level

n
2n
โœ“ 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
Asked in
Google 45 Amazon 38 Meta 32 Microsoft 28
38.5K Views
Medium Frequency
~25 min Avg. Time
1.2K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen