You are given integers height and width which specify the dimensions of a brick wall you are building. You are also given a 0-indexed array of
unique integers bricks, where the ith brick has a height of 1 and a width of bricks[i]. You have an infinite supply of each type of brick and bricks may not be rotated.
Each row in the wall must be exactly width units long. For the wall to be
sturdy , adjacent rows in the wall should not join bricks at the same location, except at the ends of the wall.
Return the number of ways to build asturdy wall. Since the answer may be very large, return it modulo109 + 7.

Input: height =2, width =3, bricks =[1,2]Output: 2Explanation:
The first two walls in the diagram show the only two ways to build a sturdy brick wall.Note that the third wall in the diagram is not sturdy because adjacent rows join bricks 2 units from the left.
Example 2:
1
2
3
4
Input: height =1, width =1, bricks =[5]Output: 0Explanation:
There are no ways to build a sturdy wall because the only type of brick we have is longer than the width of the wall.
Each row can be represented by the set of positions where bricks end (except the wall ends). For the wall to be sturdy, no two adjacent rows can have a brick end at the same position. Enumerate all valid row patterns, then use DP to build the wall row by row, ensuring adjacent rows are compatible.
classSolution {
public:int buildWall(int height, int width, vector<int>& bricks) {
constint mod =1e9+7;
vector<int> patterns;
function<void(int,int)> dfs = [&](int pos, int mask) {
if (pos == width) { patterns.push_back(mask); return; }
for (int b : bricks) {
if (pos + b > width) continue;
int nmask = mask;
if (pos + b < width) nmask |=1<< (pos + b);
dfs(pos + b, nmask);
}
};
dfs(0, 0);
int m = patterns.size();
vector<vector<int>> compat(m);
for (int i =0; i < m; ++i)
for (int j =0; j < m; ++j)
if ((patterns[i] & patterns[j]) ==0)
compat[i].push_back(j);
vector<int> dp(m, 1);
for (int h =1; h < height; ++h) {
vector<int> ndp(m);
for (int i =0; i < m; ++i)
for (int j : compat[i])
ndp[i] = (ndp[i] + dp[j]) % mod;
dp = ndp;
}
int ans =0;
for (int x : dp) ans = (ans + x) % mod;
return ans;
}
};
classSolution {
publicintbuildWall(int height, int width, int[] bricks) {
int mod = 1_000_000_007;
List<Integer> patterns =new ArrayList<>();
dfs(0, 0, width, bricks, patterns);
int m = patterns.size();
List<Integer>[] compat =new List[m];
for (int i = 0; i < m; i++) compat[i]=new ArrayList<>();
for (int i = 0; i < m; i++)
for (int j = 0; j < m; j++)
if ((patterns.get(i) & patterns.get(j)) == 0)
compat[i].add(j);
int[] dp =newint[m];
Arrays.fill(dp, 1);
for (int h = 1; h < height; h++) {
int[] ndp =newint[m];
for (int i = 0; i < m; i++)
for (int j : compat[i])
ndp[i]= (ndp[i]+ dp[j]) % mod;
dp = ndp;
}
int ans = 0;
for (int x : dp) ans = (ans + x) % mod;
return ans;
}
privatevoiddfs(int pos, int mask, int width, int[] bricks, List<Integer> patterns) {
if (pos == width) { patterns.add(mask); return; }
for (int b : bricks) {
if (pos + b > width) continue;
int nmask = mask;
if (pos + b < width) nmask |= 1 << (pos + b);
dfs(pos + b, nmask, width, bricks, patterns);
}
}
}
classSolution {
funbuildWall(height: Int, width: Int, bricks: IntArray): Int {
val mod = 1_000_000_007
val patterns = mutableListOf<Int>()
fundfs(pos: Int, mask: Int) {
if (pos == width) { patterns.add(mask); return }
for (b in bricks) {
if (pos + b > width) continuevar nmask = mask
if (pos + b < width) nmask = nmask or (1 shl (pos + b))
dfs(pos + b, nmask)
}
}
dfs(0, 0)
val m = patterns.size
val compat = Array(m) { mutableListOf<Int>() }
for (i in0 until m)
for (j in0 until m)
if ((patterns[i] and patterns[j]) ==0)
compat[i].add(j)
var dp = IntArray(m) { 1 }
for (h in1 until height) {
val ndp = IntArray(m)
for (i in0 until m)
for (j in compat[i])
ndp[i] = (ndp[i] + dp[j]) % mod
dp = ndp
}
var ans = 0for (x in dp) ans = (ans + x) % mod
return ans
}
}
classSolution:
defbuildWall(self, height: int, width: int, bricks: list[int]) -> int:
mod =10**9+7 patterns = []
defdfs(pos, mask):
if pos == width:
patterns.append(mask)
returnfor b in bricks:
if pos + b > width: continue nmask = mask
if pos + b < width: nmask |=1<< (pos + b)
dfs(pos + b, nmask)
dfs(0, 0)
m = len(patterns)
compat = [[] for _ in range(m)]
for i in range(m):
for j in range(m):
if patterns[i] & patterns[j] ==0:
compat[i].append(j)
dp = [1]*m
for _ in range(1, height):
ndp = [0]*m
for i in range(m):
for j in compat[i]:
ndp[i] = (ndp[i] + dp[j]) % mod
dp = ndp
return sum(dp) % mod