You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The figure above shows one way of covering the tiles with the carpets such that no white tiles are visible.
52
52
Note that the carpets are able to overlap one another.
53
53
</pre>
@@ -69,18 +69,18 @@ Note that the carpets are able to overlap one another.
69
69
70
70
### Solution 1: Memoization Search
71
71
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})$.
73
73
74
-
For index $i$, we discuss different cases:
74
+
For index $i$, we discuss the following cases:
75
75
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))$.
80
80
81
-
Use memoization search.
81
+
We use memoization search to solve this problem.
82
82
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.
84
84
85
85
<!-- tabs:start -->
86
86
@@ -90,10 +90,10 @@ The time complexity is $O(n\times m)$, and the space complexity is $O(n\times m)
0 commit comments