The Minimum White Tiles After Covering With Carpets problem gives you a binary string of floor tiles (1 = white, 0 = black) and a set of carpets, each of a fixed length. You can place each carpet anywhere on the floor to cover tiles. Find the minimum number of white tiles still visible after optimally placing all carpets. This hard Minimum White Tiles After Covering With Carpets coding problem uses prefix sums and dynamic programming to determine optimal carpet placements.
Google asks this because it requires combining prefix sums (to quickly compute the number of white tiles in a range) with DP (to decide where to place each carpet optimally). The string, dynamic programming, and prefix sum interview pattern is at the core, and the problem tests whether candidates can design a DP state that naturally incorporates carpet coverage decisions.
DP with prefix sums. Define dp[i][j] = minimum white tiles visible in the first i tiles using exactly j carpets. Precompute prefix sums of white tiles prefixWhite[i]. For each position i and number of carpets j:
dp[i][j] = dp[i-1][j] + (floor[i-1] == '1').dp[i][j] = dp[max(0, i - carpetLen)][j-1] (carpet covers positions [i-carpetLen+1, i]).Take minimum of both options.
Floor: "10110101", carpetLen = 2, numCarpets = 2. prefixWhite = [0,1,1,2,3,3,4,4,5].
DP problems with "place K objects to minimize/maximize" follow a standard pattern: state captures position and objects used, transitions cover "place here" and "don't place here." Prefix sums are the crucial companion — always precompute them before writing the DP. If the DP requires a range query (like "how many whites in positions [l,r]?"), prefix sums give O(1) answers. Practice DP-with-prefix-sum problems like "minimum cost painting" and "interval coverage optimization" to build this combined skillset.
| Title | Difficulty | Topics | LeetCode |
|---|---|---|---|
| Number of Beautiful Partitions | Hard | Solve | |
| Find the Original Typed String II | Hard | Solve | |
| Number of Ways to Select Buildings | Medium | Solve | |
| Jump Game VII | Medium | Solve | |
| Count The Repetitions | Hard | Solve |