Ctrl K

Staggered Brick Wall

Medium
You are building a wall of rows rows, each exactly length units long. You have bricks of height 1 with the lengths listed in pieces (all different); there is an unlimited supply of each length, and bricks cannot be rotated. The wall is solid if no two adjacent rows have a joint between bricks at the same position. The two ends of the wall do not count as joints. Count the ways to build a solid wall. Two ways differ if some row has its bricks in a different sequence. Return the answer modulo 10⁹ + 7.
Example 1
rows=2, length=4, pieces=[1, 3] → 2

A row can be 1+1+1+1, 1+3 or 3+1. Only "1+3 below 3+1" and "3+1 below 1+3" make a solid wall: their joints sit 1 and 3 units from the edge and never line up.

Example 2
rows=3, length=2, pieces=[2] → 1

Every row is a single brick of length 2 with no inner joints, so three such rows make a solid wall.

Example 3
rows=1, length=5, pieces=[3, 4] → 0

Bricks of length 3 and 4 cannot fill a row of length exactly 5.

Constraints
  • 1 ≤ rows ≤ 100
  • 1 ≤ length ≤ 10
  • 1 ≤ pieces.length ≤ 10
  • 1 ≤ pieces[i] ≤ 10
  • All values in pieces are distinct
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsSquare Frame From Sticks MediumSeating by Divisibility MediumCheapest Basket with Bundles MediumGroups With Equal Sums MediumLeast Total Walk to Scooters Medium
loading Python…
Input
rows=2, length=4, pieces=[1, 3]
Expected
2

Run checks the first 3 tests in your browser. Submit checks all 122 tests on the server, hidden ones included.