LeetCode 2209
You are given a 0-indexed binary string
floor
, which represents the colors of tiles on a floor:
floor[i] = '0'
denotes that theith
tile of the floor is colored black.- On the other hand,
floor[i] = '1'
denotes that theith
tile of the floor is colored white.You are also given
numCarpets
andcarpetLen
. You havenumCarpets
black carpets, each of lengthcarpetLen
tiles. Cover the tiles with the given carpets such that the number of white tiles still visible is minimum. Carpets may overlap one another.Return the minimum number of white tiles still visible.
At first sight, there are an infinitely huge amount of combinations available and it seems impossible to test and find out the best one. But yet again, dynamic programming comes and saves the day:
We first define a 2-D integer array: int dp[i][j], storing the minimum number of white tiles from 0-th to the i-th tile after covering j carpet. When we increment i by 1, i.e. extending the current tiles by another one, we will be finding out the minimum of:
1) Cover: Covering the current tile with a carpet, meaning no white tile from the (i - length of carpet +1) to the current one, plus the minimum