Skip to content

Commit 463bcc0

Browse files
authored
feat: add solutions to lc problem: No.2209 (#4088)
No.2209.Minimum White Tiles After Covering With Carpets
1 parent 5718c16 commit 463bcc0

File tree

6 files changed

+163
-62
lines changed

6 files changed

+163
-62
lines changed

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README.md

+56-20
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,19 @@ tags:
7373

7474
### 方法一:记忆化搜索
7575

76-
我们设计一个函数 $dfs(i, j)$ 表示从下标 $i$ 开始,使用 $j$ 条地毯,最少有多少个白色砖块没有被覆盖。答案即为 $dfs(0, numCarpets)$。
76+
我们设计一个函数 $\textit{dfs}(i, j)$ 表示从下标 $i$ 开始,使用 $j$ 条地毯,最少有多少个白色砖块没有被覆盖。答案即为 $\textit{dfs}(0, \textit{numCarpets})$。
7777

7878
对于下标 $i$,我们分情况讨论:
7979

8080
- 如果 $i \ge n$,说明已经覆盖完所有砖块,返回 $0$;
81-
- 如果 $floor[i] = 0$,则不需要使用地毯,直接跳过即可,即 $dfs(i, j) = dfs(i + 1, j)$;
82-
- 如果 $j = 0$,那么我们可以直接利用前缀和数组 $s$ 计算出剩余未被覆盖的白色砖块的数目,即 $dfs(i, j) = s[n] - s[i]$;
83-
- 如果 $floor[i] = 1$,那么我们可以选择使用地毯覆盖,也可以选择不使用地毯覆盖,取两者的最小值即可,即 $dfs(i, j) = min(dfs(i + 1, j), dfs(i + carpetLen, j - 1))$。
81+
- 如果 $\textit{floor}[i] = 0$,则不需要使用地毯,直接跳过即可,即 $\textit{dfs}(i, j) = \textit{dfs}(i + 1, j)$;
82+
- 如果 $j = 0$,那么我们可以直接利用前缀和数组 $s$ 计算出剩余未被覆盖的白色砖块的数目,即 $\textit{dfs}(i, j) = s[n] - s[i]$;
83+
- 如果 $\textit{floor}[i] = 1$,那么我们可以选择使用地毯覆盖,也可以选择不使用地毯覆盖,取两者的最小值即可,即 $\textit{dfs}(i, j) = \min(\textit{dfs}(i + 1,
84+
j), \textit{dfs}(i + \textit{carpetLen}, j - 1))$。
8485

8586
记忆化搜索即可。
8687

87-
时间复杂度 $O(n\times m)$,空间复杂度 $O(n\times m)$。其中 $n$ 和 $m$ 分别为字符串 $floor$ 的长度和 $numCarpets$ 的值。
88+
时间复杂度 $O(n\times m)$,空间复杂度 $O(n\times m)$。其中 $n$ 和 $m$ 分别为字符串 $\textit{floor}$ 的长度和 $\textit{numCarpets}$ 的值。
8889

8990
<!-- tabs:start -->
9091

@@ -94,10 +95,10 @@ tags:
9495
class Solution:
9596
def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
9697
@cache
97-
def dfs(i, j):
98+
def dfs(i: int, j: int) -> int:
9899
if i >= n:
99100
return 0
100-
if floor[i] == '0':
101+
if floor[i] == "0":
101102
return dfs(i + 1, j)
102103
if j == 0:
103104
return s[-1] - s[i]
@@ -106,7 +107,7 @@ class Solution:
106107
n = len(floor)
107108
s = [0] * (n + 1)
108109
for i, c in enumerate(floor):
109-
s[i + 1] = s[i] + int(c == '1')
110+
s[i + 1] = s[i] + int(c == "1")
110111
ans = dfs(0, numCarpets)
111112
dfs.cache_clear()
112113
return ans
@@ -116,17 +117,14 @@ class Solution:
116117

117118
```java
118119
class Solution {
119-
private int[][] f;
120+
private Integer[][] f;
120121
private int[] s;
121122
private int n;
122123
private int k;
123124

124125
public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
125126
n = floor.length();
126-
f = new int[n][numCarpets + 1];
127-
for (var e : f) {
128-
Arrays.fill(e, -1);
129-
}
127+
f = new Integer[n][numCarpets + 1];
130128
s = new int[n + 1];
131129
for (int i = 0; i < n; ++i) {
132130
s[i + 1] = s[i] + (floor.charAt(i) == '1' ? 1 : 0);
@@ -142,7 +140,7 @@ class Solution {
142140
if (j == 0) {
143141
return s[n] - s[i];
144142
}
145-
if (f[i][j] != -1) {
143+
if (f[i][j] != null) {
146144
return f[i][j];
147145
}
148146
if (s[i + 1] == s[i]) {
@@ -167,12 +165,19 @@ public:
167165
for (int i = 0; i < n; ++i) {
168166
s[i + 1] = s[i] + (floor[i] == '1');
169167
}
170-
function<int(int, int)> dfs;
171-
dfs = [&](int i, int j) {
172-
if (i >= n) return 0;
173-
if (j == 0) return s[n] - s[i];
174-
if (f[i][j] != -1) return f[i][j];
175-
if (s[i + 1] == s[i]) return dfs(i + 1, j);
168+
auto dfs = [&](this auto&& dfs, int i, int j) -> int {
169+
if (i >= n) {
170+
return 0;
171+
}
172+
if (j == 0) {
173+
return s[n] - s[i];
174+
}
175+
if (f[i][j] != -1) {
176+
return f[i][j];
177+
}
178+
if (s[i + 1] == s[i]) {
179+
return dfs(i + 1, j);
180+
}
176181
int ans = min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1));
177182
f[i][j] = ans;
178183
return ans;
@@ -220,6 +225,37 @@ func minimumWhiteTiles(floor string, numCarpets int, carpetLen int) int {
220225
}
221226
```
222227

228+
#### TypeScript
229+
230+
```ts
231+
function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number): number {
232+
const n = floor.length;
233+
const f: number[][] = Array.from({ length: n }, () => Array(numCarpets + 1).fill(-1));
234+
const s: number[] = Array(n + 1).fill(0);
235+
for (let i = 0; i < n; ++i) {
236+
s[i + 1] = s[i] + (floor[i] === '1' ? 1 : 0);
237+
}
238+
const dfs = (i: number, j: number): number => {
239+
if (i >= n) {
240+
return 0;
241+
}
242+
if (j === 0) {
243+
return s[n] - s[i];
244+
}
245+
if (f[i][j] !== -1) {
246+
return f[i][j];
247+
}
248+
if (s[i + 1] === s[i]) {
249+
return dfs(i + 1, j);
250+
}
251+
const ans = Math.min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1));
252+
f[i][j] = ans;
253+
return ans;
254+
};
255+
return dfs(0, numCarpets);
256+
}
257+
```
258+
223259
<!-- tabs:end -->
224260

225261
<!-- solution:end -->

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/README_EN.md

+60-25
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ tags:
3737
<pre>
3838
<strong>Input:</strong> floor = &quot;10110101&quot;, numCarpets = 2, carpetLen = 2
3939
<strong>Output:</strong> 2
40-
<strong>Explanation:</strong>
40+
<strong>Explanation:</strong>
4141
The figure above shows one way of covering the tiles with the carpets such that only 2 white tiles are visible.
4242
No other way of covering the tiles with the carpets can leave less than 2 white tiles visible.
4343
</pre>
@@ -47,7 +47,7 @@ No other way of covering the tiles with the carpets can leave less than 2 white
4747
<pre>
4848
<strong>Input:</strong> floor = &quot;11111&quot;, numCarpets = 2, carpetLen = 3
4949
<strong>Output:</strong> 0
50-
<strong>Explanation:</strong>
50+
<strong>Explanation:</strong>
5151
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.
5252
Note that the carpets are able to overlap one another.
5353
</pre>
@@ -69,18 +69,18 @@ Note that the carpets are able to overlap one another.
6969

7070
### Solution 1: Memoization Search
7171

72-
Design a function $dfs(i, j)$ to represent the minimum number of white bricks that are not covered starting from index $i$ using $j$ carpets. The answer is $dfs(0, numCarpets)$.
72+
We design a function $\textit{dfs}(i, j)$ to represent the minimum number of white tiles that are not covered starting from index $i$ using $j$ carpets. The answer is $\textit{dfs}(0, \textit{numCarpets})$.
7373

74-
For index $i$, we discuss different cases:
74+
For index $i$, we discuss the following cases:
7575

76-
- If $i \ge n$, it means that all bricks have been covered, return $0$;
77-
- If $floor[i] = 0$, there is no need to use a carpet, just skip it, that is, $dfs(i, j) = dfs(i + 1, j)$;
78-
- If $j = 0$, we can directly calculate the number of remaining white bricks that have not been covered using the prefix sum array $s$, that is, $dfs(i, j) = s[n] - s[i]$;
79-
- If $floor[i] = 1$, we can choose to use a carpet to cover it, or choose not to use a carpet to cover it, and take the minimum of the two, that is, $dfs(i, j) = min(dfs(i + 1, j), dfs(i + carpetLen, j - 1))$.
76+
- If $i \ge n$, it means all tiles have been covered, return $0$;
77+
- If $\textit{floor}[i] = 0$, then we do not need to use a carpet, just skip it, i.e., $\textit{dfs}(i, j) = \textit{dfs}(i + 1, j)$;
78+
- If $j = 0$, then we can directly use the prefix sum array $s$ to calculate the number of remaining uncovered white tiles, i.e., $\textit{dfs}(i, j) = s[n] - s[i]$;
79+
- If $\textit{floor}[i] = 1$, then we can choose to use a carpet or not, and take the minimum of the two, i.e., $\textit{dfs}(i, j) = \min(\textit{dfs}(i + 1, j), \textit{dfs}(i + \textit{carpetLen}, j - 1))$.
8080

81-
Use memoization search.
81+
We use memoization search to solve this problem.
8282

83-
The time complexity is $O(n\times m)$, and the space complexity is $O(n\times m)$. Where $n$ and $m$ are the lengths of the string $floor$ and the value of $numCarpets$ respectively.
83+
The time complexity is $O(n \times m)$, and the space complexity is $O(n \times m)$. Here, $n$ and $m$ are the length of the string $\textit{floor}$ and the value of $\textit{numCarpets}$, respectively.
8484

8585
<!-- tabs:start -->
8686

@@ -90,10 +90,10 @@ The time complexity is $O(n\times m)$, and the space complexity is $O(n\times m)
9090
class Solution:
9191
def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
9292
@cache
93-
def dfs(i, j):
93+
def dfs(i: int, j: int) -> int:
9494
if i >= n:
9595
return 0
96-
if floor[i] == '0':
96+
if floor[i] == "0":
9797
return dfs(i + 1, j)
9898
if j == 0:
9999
return s[-1] - s[i]
@@ -102,7 +102,7 @@ class Solution:
102102
n = len(floor)
103103
s = [0] * (n + 1)
104104
for i, c in enumerate(floor):
105-
s[i + 1] = s[i] + int(c == '1')
105+
s[i + 1] = s[i] + int(c == "1")
106106
ans = dfs(0, numCarpets)
107107
dfs.cache_clear()
108108
return ans
@@ -112,17 +112,14 @@ class Solution:
112112

113113
```java
114114
class Solution {
115-
private int[][] f;
115+
private Integer[][] f;
116116
private int[] s;
117117
private int n;
118118
private int k;
119119

120120
public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
121121
n = floor.length();
122-
f = new int[n][numCarpets + 1];
123-
for (var e : f) {
124-
Arrays.fill(e, -1);
125-
}
122+
f = new Integer[n][numCarpets + 1];
126123
s = new int[n + 1];
127124
for (int i = 0; i < n; ++i) {
128125
s[i + 1] = s[i] + (floor.charAt(i) == '1' ? 1 : 0);
@@ -138,7 +135,7 @@ class Solution {
138135
if (j == 0) {
139136
return s[n] - s[i];
140137
}
141-
if (f[i][j] != -1) {
138+
if (f[i][j] != null) {
142139
return f[i][j];
143140
}
144141
if (s[i + 1] == s[i]) {
@@ -163,12 +160,19 @@ public:
163160
for (int i = 0; i < n; ++i) {
164161
s[i + 1] = s[i] + (floor[i] == '1');
165162
}
166-
function<int(int, int)> dfs;
167-
dfs = [&](int i, int j) {
168-
if (i >= n) return 0;
169-
if (j == 0) return s[n] - s[i];
170-
if (f[i][j] != -1) return f[i][j];
171-
if (s[i + 1] == s[i]) return dfs(i + 1, j);
163+
auto dfs = [&](this auto&& dfs, int i, int j) -> int {
164+
if (i >= n) {
165+
return 0;
166+
}
167+
if (j == 0) {
168+
return s[n] - s[i];
169+
}
170+
if (f[i][j] != -1) {
171+
return f[i][j];
172+
}
173+
if (s[i + 1] == s[i]) {
174+
return dfs(i + 1, j);
175+
}
172176
int ans = min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1));
173177
f[i][j] = ans;
174178
return ans;
@@ -216,6 +220,37 @@ func minimumWhiteTiles(floor string, numCarpets int, carpetLen int) int {
216220
}
217221
```
218222

223+
#### TypeScript
224+
225+
```ts
226+
function minimumWhiteTiles(floor: string, numCarpets: number, carpetLen: number): number {
227+
const n = floor.length;
228+
const f: number[][] = Array.from({ length: n }, () => Array(numCarpets + 1).fill(-1));
229+
const s: number[] = Array(n + 1).fill(0);
230+
for (let i = 0; i < n; ++i) {
231+
s[i + 1] = s[i] + (floor[i] === '1' ? 1 : 0);
232+
}
233+
const dfs = (i: number, j: number): number => {
234+
if (i >= n) {
235+
return 0;
236+
}
237+
if (j === 0) {
238+
return s[n] - s[i];
239+
}
240+
if (f[i][j] !== -1) {
241+
return f[i][j];
242+
}
243+
if (s[i + 1] === s[i]) {
244+
return dfs(i + 1, j);
245+
}
246+
const ans = Math.min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1));
247+
f[i][j] = ans;
248+
return ans;
249+
};
250+
return dfs(0, numCarpets);
251+
}
252+
```
253+
219254
<!-- tabs:end -->
220255

221256
<!-- solution:end -->

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/Solution.cpp

+14-7
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,23 @@ class Solution {
77
for (int i = 0; i < n; ++i) {
88
s[i + 1] = s[i] + (floor[i] == '1');
99
}
10-
function<int(int, int)> dfs;
11-
dfs = [&](int i, int j) {
12-
if (i >= n) return 0;
13-
if (j == 0) return s[n] - s[i];
14-
if (f[i][j] != -1) return f[i][j];
15-
if (s[i + 1] == s[i]) return dfs(i + 1, j);
10+
auto dfs = [&](this auto&& dfs, int i, int j) -> int {
11+
if (i >= n) {
12+
return 0;
13+
}
14+
if (j == 0) {
15+
return s[n] - s[i];
16+
}
17+
if (f[i][j] != -1) {
18+
return f[i][j];
19+
}
20+
if (s[i + 1] == s[i]) {
21+
return dfs(i + 1, j);
22+
}
1623
int ans = min(1 + dfs(i + 1, j), dfs(i + carpetLen, j - 1));
1724
f[i][j] = ans;
1825
return ans;
1926
};
2027
return dfs(0, numCarpets);
2128
}
22-
};
29+
};

solution/2200-2299/2209.Minimum White Tiles After Covering With Carpets/Solution.java

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,12 @@
11
class Solution {
2-
private int[][] f;
2+
private Integer[][] f;
33
private int[] s;
44
private int n;
55
private int k;
66

77
public int minimumWhiteTiles(String floor, int numCarpets, int carpetLen) {
88
n = floor.length();
9-
f = new int[n][numCarpets + 1];
10-
for (var e : f) {
11-
Arrays.fill(e, -1);
12-
}
9+
f = new Integer[n][numCarpets + 1];
1310
s = new int[n + 1];
1411
for (int i = 0; i < n; ++i) {
1512
s[i + 1] = s[i] + (floor.charAt(i) == '1' ? 1 : 0);
@@ -25,7 +22,7 @@ private int dfs(int i, int j) {
2522
if (j == 0) {
2623
return s[n] - s[i];
2724
}
28-
if (f[i][j] != -1) {
25+
if (f[i][j] != null) {
2926
return f[i][j];
3027
}
3128
if (s[i + 1] == s[i]) {
@@ -35,4 +32,4 @@ private int dfs(int i, int j) {
3532
f[i][j] = ans;
3633
return ans;
3734
}
38-
}
35+
}
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
class Solution:
22
def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int:
33
@cache
4-
def dfs(i, j):
4+
def dfs(i: int, j: int) -> int:
55
if i >= n:
66
return 0
7-
if floor[i] == '0':
7+
if floor[i] == "0":
88
return dfs(i + 1, j)
99
if j == 0:
1010
return s[-1] - s[i]
@@ -13,7 +13,7 @@ def dfs(i, j):
1313
n = len(floor)
1414
s = [0] * (n + 1)
1515
for i, c in enumerate(floor):
16-
s[i + 1] = s[i] + int(c == '1')
16+
s[i + 1] = s[i] + int(c == "1")
1717
ans = dfs(0, numCarpets)
1818
dfs.cache_clear()
1919
return ans

0 commit comments

Comments
 (0)