2
def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:4
# Using memo table to store predefined computations5
memo = [[-1 for x in range(numCarpets + 1)] for x in range(len(floor) + 1)]7
def solve(N, numCarpets):11
# If calculated previously use that solution12
if memo[N][numCarpets] != -1:13
return memo[N][numCarpets]15
used = 0 # If you use the carpet16
notused = 0 # If you donot use the carpet18
if floor[N] == "1": # We might use the carpet in this part19
if numCarpets > 0: # Whether we even have some carpets or not22
We are finding the number of ones present in this part of the floor.23
prefix[lastInd] - Number of ones till lastInd24
prefix[N] - Number of ones till Nth Index.26
Their difference gives us how many ones present between the two.28
lastInd = min(N + carpetLen, len(floor))29
ans = prefix[lastInd] - prefix[N]32
Find the max if we use or donot use carpet at this index33
If we do we add --- ans and decrement remaining carpets37
solve(N + carpetLen, numCarpets - 1) + ans,38
solve(N + 1, numCarpets),45
# If we donot use the carpet although I feel this might be redundant code46
notused = solve(N + 1, numCarpets)48
# Using max function to find the number of white tiles removed49
memo[N][numCarpets] = max(used, notused)50
return memo[N][numCarpets]59
# Using Prefix array to store number of ones till i th index60
prefix = [0] * (n + 1)61
for i in range(1, n + 1):62
if floor[i - 1] == "1":63
prefix[i] = prefix[i - 1] + 165
prefix[i] = prefix[i - 1]67
removed = solve(0, numCarpets)