1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-01-10 18:48:13 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
leetcode-problemset/leetcode-cn/problem (Chinese)/粉刷房子 III [paint-house-iii].html
2022-03-29 12:43:11 +08:00

64 lines
2.8 KiB
HTML
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<p>在一个小城市里,有 <code>m</code> 个房子排成一排,你需要给每个房子涂上 <code>n</code> 种颜色之一(颜色编号为 <code>1</code><code>n</code> )。有的房子去年夏天已经涂过颜色了,所以这些房子不可以被重新涂色。</p>
<p>我们将连续相同颜色尽可能多的房子称为一个街区。(比方说 <code>houses = [1,2,2,3,3,2,1,1]</code> ,它包含 5 个街区 <code> [{1}, {2,2}, {3,3}, {2}, {1,1}]</code> 。)</p>
<p>给你一个数组 <code>houses</code> ,一个 <code>m * n</code> 的矩阵 <code>cost</code> 和一个整数 <code>target</code> ,其中:</p>
<ul>
<li><code>houses[i]</code>:是第 <code>i</code> 个房子的颜色,<strong>0</strong> 表示这个房子还没有被涂色。</li>
<li><code>cost[i][j]</code>:是将第 <code>i</code> 个房子涂成颜色 <code>j+1</code> 的花费。</li>
</ul>
<p>请你返回房子涂色方案的最小总花费,使得每个房子都被涂色后,恰好组成 <code>target</code> 个街区。如果没有可用的涂色方案,请返回 <strong>-1</strong> 。</p>
<p> </p>
<p><strong>示例 1</strong></p>
<pre>
<strong>输入:</strong>houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
<strong>输出:</strong>9
<strong>解释:</strong>房子涂色方案为 [1,2,2,1,1]
此方案包含 target = 3 个街区,分别是 [{1}, {2,2}, {1,1}]。
涂色的总花费为 (1 + 1 + 1 + 1 + 5) = 9。
</pre>
<p><strong>示例 2</strong></p>
<pre>
<strong>输入:</strong>houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
<strong>输出:</strong>11
<strong>解释:</strong>有的房子已经被涂色了,在此基础上涂色方案为 [2,2,1,2,2]
此方案包含 target = 3 个街区,分别是 [{2,2}, {1}, {2,2}]。
给第一个和最后一个房子涂色的花费为 (10 + 1) = 11。
</pre>
<p><strong>示例 3</strong></p>
<pre>
<strong>输入:</strong>houses = [0,0,0,0,0], cost = [[1,10],[10,1],[1,10],[10,1],[1,10]], m = 5, n = 2, target = 5
<strong>输出:</strong>5
</pre>
<p><strong>示例 4</strong></p>
<pre>
<strong>输入:</strong>houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3
<strong>输出:</strong>-1
<strong>解释:</strong>房子已经被涂色并组成了 4 个街区,分别是 [{3},{1},{2},{3}] ,无法形成 target = 3 个街区。
</pre>
<p> </p>
<p><strong>提示:</strong></p>
<ul>
<li><code>m == houses.length == cost.length</code></li>
<li><code>n == cost[i].length</code></li>
<li><code>1 <= m <= 100</code></li>
<li><code>1 <= n <= 20</code></li>
<li><code>1 <= target <= m</code></li>
<li><code>0 <= houses[i] <= n</code></li>
<li><code>1 <= cost[i][j] <= 10^4</code></li>
</ul>