|
| 1 | +## 题目地址(2209. 用地毯覆盖后的最少白色砖块) |
| 2 | + |
| 3 | +https://leetcode-cn.com/problems/minimum-white-tiles-after-covering-with-carpets/ |
| 4 | + |
| 5 | +## 题目描述 |
| 6 | + |
| 7 | +``` |
| 8 | +给你一个下标从 0 开始的 二进制 字符串 floor ,它表示地板上砖块的颜色。 |
| 9 | +
|
| 10 | +floor[i] = '0' 表示地板上第 i 块砖块的颜色是 黑色 。 |
| 11 | +floor[i] = '1' 表示地板上第 i 块砖块的颜色是 白色 。 |
| 12 | +
|
| 13 | +同时给你 numCarpets 和 carpetLen 。你有 numCarpets 条 黑色 的地毯,每一条 黑色 的地毯长度都为 carpetLen 块砖块。请你使用这些地毯去覆盖砖块,使得未被覆盖的剩余 白色 砖块的数目 最小 。地毯相互之间可以覆盖。 |
| 14 | +
|
| 15 | +请你返回没被覆盖的白色砖块的 最少 数目。 |
| 16 | +
|
| 17 | + |
| 18 | +
|
| 19 | +示例 1: |
| 20 | +
|
| 21 | +输入:floor = "10110101", numCarpets = 2, carpetLen = 2 |
| 22 | +输出:2 |
| 23 | +解释: |
| 24 | +上图展示了剩余 2 块白色砖块的方案。 |
| 25 | +没有其他方案可以使未被覆盖的白色砖块少于 2 块。 |
| 26 | +
|
| 27 | +
|
| 28 | +示例 2: |
| 29 | +
|
| 30 | +输入:floor = "11111", numCarpets = 2, carpetLen = 3 |
| 31 | +输出:0 |
| 32 | +解释: |
| 33 | +上图展示了所有白色砖块都被覆盖的一种方案。 |
| 34 | +注意,地毯相互之间可以覆盖。 |
| 35 | +
|
| 36 | +
|
| 37 | + |
| 38 | +
|
| 39 | +提示: |
| 40 | +
|
| 41 | +1 <= carpetLen <= floor.length <= 1000 |
| 42 | +floor[i] 要么是 '0' ,要么是 '1' 。 |
| 43 | +1 <= numCarpets <= 1000 |
| 44 | +``` |
| 45 | + |
| 46 | +## 前置知识 |
| 47 | + |
| 48 | +- 动态规划 |
| 49 | + |
| 50 | +## 公司 |
| 51 | + |
| 52 | +- 暂无 |
| 53 | + |
| 54 | +## 思路 |
| 55 | + |
| 56 | +定义 dp[i][j] 为仅考虑前 i 个砖块,使用 j 块毯子 的最少漏出白色砖块数目。 |
| 57 | + |
| 58 | +那么答案自然就是 dp[-1][-1] |
| 59 | + |
| 60 | +我们考虑如何转移,和大多数转移方程一样,实际上就是一个选择问题。 |
| 61 | + |
| 62 | +- 如果选择盖住 floor[i] (不妨毯子尾部盖住 floor[i]),那么 dp[i][j] = dp[i - |
| 63 | + carpetLen][j - 1] |
| 64 | +- 如果选择不盖住 floor[i],那么 dp[i][j] = dp[i - 1][j] + int(floor[i] == '1')。 |
| 65 | + 其中 int(floor[i] == '1') 表示如果 floor[i] 是黑的,那么漏出白色不受影响(+0) |
| 66 | + ,否则漏出白色多一块(+1)。 |
| 67 | + |
| 68 | +最后考虑 base case。 |
| 69 | + |
| 70 | +当 j == 0(没有毯子可用),我们如何考虑?此时: |
| 71 | + |
| 72 | +```py |
| 73 | +dp[i][j] = dp[i-1][j] + int(floor[i] == '1') |
| 74 | +``` |
| 75 | + |
| 76 | +那么当 i == 0 ,需要特殊考虑么?在这里是不需要的。 |
| 77 | + |
| 78 | +## 关键点 |
| 79 | + |
| 80 | +- |
| 81 | + |
| 82 | +## 代码 |
| 83 | + |
| 84 | +- 语言支持:Python3 |
| 85 | + |
| 86 | +Python3 Code: |
| 87 | + |
| 88 | +```python |
| 89 | + |
| 90 | +class Solution: |
| 91 | + def minimumWhiteTiles(self, floor: str, numCarpets: int, carpetLen: int) -> int: |
| 92 | + dp = [[0] * (numCarpets + 1) for _ in range(len(floor))] |
| 93 | + for i in range(len(floor)): |
| 94 | + for j in range(numCarpets + 1): |
| 95 | + if j == 0: |
| 96 | + dp[i][j] = dp[i-1][j] + int(floor[i] == '1') |
| 97 | + continue |
| 98 | + if i >= carpetLen and j > 0: |
| 99 | + dp[i][j] = dp[i - carpetLen][j - 1] |
| 100 | + dp[i][j] = min(dp[i][j], dp[i-1][j] + int(floor[i] == '1')) |
| 101 | + |
| 102 | + return dp[-1][-1] |
| 103 | + |
| 104 | +``` |
| 105 | + |
| 106 | +**复杂度分析** |
| 107 | + |
| 108 | +令 n 为 floor 长度。 |
| 109 | + |
| 110 | +- 时间复杂度:$O(n * numCarpets)$ |
| 111 | +- 空间复杂度:$O(n * numCarpets)$ |
| 112 | + |
| 113 | +> 此题解由 |
| 114 | +> [力扣刷题插件](https://leetcode-pp.github.io/leetcode-cheat/?tab=solution-template) |
| 115 | +> 自动生成。 |
| 116 | +
|
| 117 | +力扣的小伙伴可以[关注我](https://leetcode-cn.com/u/fe-lucifer/),这样就会第一时 |
| 118 | +间收到我的动态啦~ |
| 119 | + |
| 120 | +以上就是本文的全部内容了。大家对此有何看法,欢迎给我留言,我有时间都会一一查看回 |
| 121 | +答。更多算法套路可以访问我的 LeetCode 题解仓库 |
| 122 | +:https://github.com/azl397985856/leetcode 。 目前已经 40K star 啦。大家也可以关 |
| 123 | +注我的公众号《力扣加加》带你啃下算法这块硬骨头。 |
| 124 | + |
| 125 | +关注公众号力扣加加,努力用清晰直白的语言还原解题思路,并且有大量图解,手把手教你 |
| 126 | +识别套路,高效刷题。 |
| 127 | + |
| 128 | + |
0 commit comments