Skip to content

Commit 2ff0661

Browse files
authored
Update 2184-number-of-ways-to-build-sturdy-brick-wall.js
1 parent a6015ed commit 2ff0661

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed

2184-number-of-ways-to-build-sturdy-brick-wall.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1+
/**
2+
* @param {number} height
3+
* @param {number} width
4+
* @param {number[]} bricks
5+
* @return {number}
6+
*/
7+
const buildWall = function (height, width, bricks) {
8+
const mod = 1e9 + 7
9+
const avail = []
10+
const bset = new Set(bricks)
11+
const m = width - 1, limit = 1 << m
12+
for(let mask = 0; mask < limit; mask++) {
13+
const idxArr = [-1]
14+
for(let j = 0; j < m; j++) {
15+
if((mask >> j) & 1) idxArr.push(j)
16+
}
17+
idxArr.push(m)
18+
let flag = true
19+
for(let j = 1, len = idxArr.length; j < len; j++) {
20+
if(!bset.has(idxArr[j] - idxArr[j - 1])) {
21+
flag = false
22+
break
23+
}
24+
}
25+
if(flag) avail.push(mask)
26+
}
27+
28+
let res = 0
29+
if(height === 1) return avail.length
30+
const dp = Array.from({ length: height }, () => Array(limit).fill(0))
31+
for(const mask of avail) {
32+
dp[0][mask] = 1
33+
}
34+
35+
for(let i = 1; i < height; i++) {
36+
for(let j = 0, len = avail.length; j < len; j++) {
37+
const cur = avail[j]
38+
for(let k = 0; k < len; k++) {
39+
const pre = avail[k]
40+
if((cur & pre) === 0) {
41+
dp[i][cur] = (dp[i][cur] + dp[i - 1][pre]) % mod
42+
}
43+
}
44+
if(i === height - 1) {
45+
res = (res + dp[i][cur]) % mod
46+
}
47+
}
48+
}
49+
50+
return res
51+
}
52+
53+
// another
54+
155
/**
256
* @param {number} height
357
* @param {number} width

0 commit comments

Comments
 (0)