You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor:
floor[i] = '0' denotes that the ith tile of the floor is colored black.
On the other hand, floor[i] = '1' denotes that the ith tile of the floor is colored white.
You are also given numCarpets and carpetLen. You have numCarpetsblack carpets, each of length carpetLen 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.
Example 1:
Input: floor = "10110101", numCarpets = 2, carpetLen = 2
Output: 2
Explanation:
The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible.
No other way of covering the tiles with the carpets can leave less than 2 white tiles visible.
Example 2:
Input: floor = "11111", numCarpets = 2, carpetLen = 3
Output: 0
Explanation:
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.
Note that the carpets are able to overlap one another.
Problem summary: You are given a 0-indexed binary string floor, which represents the colors of tiles on a floor: floor[i] = '0' denotes that the ith tile of the floor is colored black. On the other hand, floor[i] = '1' denotes that the ith tile of the floor is colored white. You are also given numCarpets and carpetLen. You have numCarpets black carpets, each of length carpetLen 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.
Baseline thinking
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Dynamic Programming
Example 1
"10110101"
2
2
Example 2
"11111"
2
3
Related Problems
Edit Distance (edit-distance)
Step 02
Core Insight
What unlocks the optimal approach
Can you think of a DP solution?
Let DP[i][j] denote the minimum number of white tiles still visible from indices i to floor.length-1 after covering with at most j carpets.
The transition will be whether to put down the carpet at position i (if possible), or not.
Interview move: turn each hint into an invariant you can check after every iteration/recursion step.
Step 03
Algorithm Walkthrough
Iteration Checklist
Define state (indices, window, stack, map, DP cell, or recursion frame).
Apply one transition step and update the invariant.
Record answer candidate when condition is met.
Continue until all input is consumed.
Use the first example testcase as your mental trace to verify each transition.
Step 04
Edge Cases
Minimum Input
Single element / shortest valid input
Validate boundary behavior before entering the main loop or recursion.
Duplicates & Repeats
Repeated values / repeated states
Decide whether duplicates should be merged, skipped, or counted explicitly.
Extreme Constraints
Largest constraint values
Re-check complexity target against constraints to avoid time-limit issues.
Invalid / Corner Shape
Empty collections, zeros, or disconnected structures
Handle special-case structure before the core algorithm path.
Step 05
Full Annotated Code
Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #2209: Minimum White Tiles After Covering With Carpets
class Solution {
private Integer[][] f;
private int[] s;
private int n;
private int k;
public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
n = floor.length();
f = new Integer[n][numCarpets + 1];
s = new int[n + 1];
for (int i = 0; i < n; ++i) {
s[i + 1] = s[i] + (floor.charAt(i) == '1' ? 1 : 0);
}
k = carpetLen;
return dfs(0, numCarpets);
}
private int dfs(int i, int j) {
if (i >= n) {
return 0;
}
if (j == 0) {
return s[n] - s[i];
}
if (f[i][j] != null) {
return f[i][j];
}
if (s[i + 1] == s[i]) {
return dfs(i + 1, j);
}
int ans = Math.min(1 + dfs(i + 1, j), dfs(i + k, j - 1));
f[i][j] = ans;
return ans;
}
}
// Accepted solution for LeetCode #2209: Minimum White Tiles After Covering With Carpets
func minimumWhiteTiles(floor string, numCarpets int, carpetLen int) int {
n := len(floor)
f := make([][]int, n)
for i := range f {
f[i] = make([]int, numCarpets+1)
for j := range f[i] {
f[i][j] = -1
}
}
s := make([]int, n+1)
for i, c := range floor {
s[i+1] = s[i] + int(c-'0')
}
var dfs func(i, j int) int
dfs = func(i, j int) int {
if i >= n {
return 0
}
if j == 0 {
return s[n] - s[i]
}
if f[i][j] != -1 {
return f[i][j]
}
if s[i+1] == s[i] {
return dfs(i+1, j)
}
ans := min(1+dfs(i+1, j), dfs(i+carpetLen, j-1))
f[i][j] = ans
return ans
}
return dfs(0, numCarpets)
}
# Accepted solution for LeetCode #2209: Minimum White Tiles After Covering With Carpets
class Solution:
def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
@cache
def dfs(i: int, j: int) -> int:
if i >= n:
return 0
if floor[i] == "0":
return dfs(i + 1, j)
if j == 0:
return s[-1] - s[i]
return min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1))
n = len(floor)
s = [0] * (n + 1)
for i, c in enumerate(floor):
s[i + 1] = s[i] + int(c == "1")
ans = dfs(0, numCarpets)
dfs.cache_clear()
return ans
// Accepted solution for LeetCode #2209: Minimum White Tiles After Covering With Carpets
impl Solution {
pub fn minimum_white_tiles(floor: String, num_carpets: i32, carpet_len: i32) -> i32 {
let n = floor.len();
let a: Vec<u8> = floor.bytes().collect();
let m = num_carpets as usize;
let k = carpet_len as usize;
let mut s = vec![0i32; n + 1];
for i in 0..n {
s[i + 1] = s[i] + if a[i] == b'1' { 1 } else { 0 };
}
let mut f = vec![vec![-1; m + 1]; n];
fn dfs(
i: usize,
j: usize,
n: usize,
k: usize,
s: &Vec<i32>,
f: &mut Vec<Vec<i32>>,
a: &Vec<u8>,
) -> i32 {
if i >= n {
return 0;
}
if j == 0 {
return s[n] - s[i];
}
if f[i][j] != -1 {
return f[i][j];
}
if s[i + 1] == s[i] {
let v = dfs(i + 1, j, n, k, s, f, a);
f[i][j] = v;
return v;
}
let t1 = 1 + dfs(i + 1, j, n, k, s, f, a);
let ni = i + k;
let t2 = dfs(ni, j - 1, n, k, s, f, a);
let t = t1.min(t2);
f[i][j] = t;
t
}
dfs(0, m, n, k, &s, &mut f, &a)
}
}
// Accepted solution for LeetCode #2209: Minimum White Tiles After Covering With Carpets
function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number): number {
const n = floor.length;
const f: number[][] = Array.from({ length: n }, () => Array(numCarpets + 1).fill(-1));
const s: number[] = Array(n + 1).fill(0);
for (let i = 0; i < n; ++i) {
s[i + 1] = s[i] + (floor[i] === '1' ? 1 : 0);
}
const dfs = (i: number, j: number): number => {
if (i >= n) {
return 0;
}
if (j === 0) {
return s[n] - s[i];
}
if (f[i][j] !== -1) {
return f[i][j];
}
if (s[i + 1] === s[i]) {
return dfs(i + 1, j);
}
const ans = Math.min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1));
f[i][j] = ans;
return ans;
};
return dfs(0, numCarpets);
}
Step 06
Interactive Study Demo
Use this to step through a reusable interview workflow for this problem.
Press Step or Run All to begin.
Step 07
Complexity Analysis
Time
O(n × m)
Space
O(n × m)
Approach Breakdown
RECURSIVE
O(2ⁿ) time
O(n) space
Pure recursion explores every possible choice at each step. With two choices per state (take or skip), the decision tree has 2ⁿ leaves. The recursion stack uses O(n) space. Many subproblems are recomputed exponentially many times.
DYNAMIC PROGRAMMING
O(n × m) time
O(n × m) space
Each cell in the DP table is computed exactly once from previously solved subproblems. The table dimensions determine both time and space. Look for the state variables — each unique combination of state values is one cell. Often a rolling array can reduce space by one dimension.
Shortcut: Count your DP state dimensions → that’s your time. Can you drop one? That’s your space optimization.
Coach Notes
Common Mistakes
Review these before coding to avoid predictable interview regressions.
State misses one required dimension
Wrong move: An incomplete state merges distinct subproblems and caches incorrect answers.
Usually fails on: Correctness breaks on cases that differ only in hidden state.
Fix: Define state so each unique subproblem maps to one DP cell.