Minimum White Tiles After Covering With Carpets LeetCode solution in JavaScript

1
var minimumWhiteTiles = function (floor, numCarpets, carpetLen) {
2
let answer = Number.MAX_SAFE_INTEGER,
3
dp = {};
4

5
floor = floor.split("");
6

7
function findMaxCoveredTiles(index, carpetsLeft) {
8
if (index >= floor.length) {
9
return 0;
10
}
11
if (dp[index]) {
12
if (dp[index][carpetsLeft] != undefined) {
13
return dp[index][carpetsLeft];
14
}
15
} else {
16
dp[index] = {};
17
}
18

19
dp[index][carpetsLeft] = Number.MIN_SAFE_INTEGER;
20
if (carpetsLeft) {
21
dp[index][carpetsLeft] =
22
Math.min(carpetLen, floor.length - index) +
23
findMaxCoveredTiles(index + carpetLen, carpetsLeft - 1);
24
}
25

26
dp[index][carpetsLeft] = Math.max(
27
dp[index][carpetsLeft],
28
!+floor[index] + findMaxCoveredTiles(index + 1, carpetsLeft)
29
);
30

31
return dp[index][carpetsLeft];
32
}
33

34
return floor.length - findMaxCoveredTiles(0, numCarpets);
35
};

0

WPM •0 •0

100%

ACC •0 •0

0s

TIME •0