Skip to content

Commit d6a84aa

Browse files
authored
Update 2209-minimum-white-tiles-after-covering-with-carpets.js
1 parent db80dd5 commit d6a84aa

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

2209-minimum-white-tiles-after-covering-with-carpets.js

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,15 @@ const minimumWhiteTiles = function(floor, numCarpets, carpetLen) {
1111
// when using j tiles to cover the first i tiles
1212
const dp = Array.from({ length: n + 1 }, () => Array(numCarpets + 1).fill(0))
1313

14+
const ones = Array(n + 1).fill(0)
1415
for(let i = 1; i <= n; i++) {
15-
for(let j = 0; j <= numCarpets; j++) {
16+
ones[i] = ones[i - 1] + (floor[i - 1] === '1' ? 1 : 0)
17+
}
18+
for(let i = 1; i <= n; i++) {
19+
dp[i][0] = ones[i]
20+
for(let j = 1; j <= numCarpets; j++) {
1621
const skip = dp[i - 1][j] + (floor[i - 1] === '1' ? 1 : 0)
17-
const cover = j > 0 ? dp[Math.max(i - carpetLen, 0)][j - 1] : 1000
22+
const cover = dp[Math.max(i - carpetLen, 0)][j - 1]
1823
dp[i][j] = Math.min(skip, cover)
1924
}
2025
}

0 commit comments

Comments
 (0)