1
0
mirror of https://gitee.com/coder-xiaomo/leetcode-problemset synced 2025-09-07 00:11:41 +08:00
Code Issues Projects Releases Wiki Activity GitHub Gitee
This commit is contained in:
2022-05-02 23:44:12 +08:00
parent 7ea03594b3
commit 2a71c78585
4790 changed files with 11696 additions and 10944 deletions

View File

@@ -12,7 +12,7 @@
"translatedContent": "<p>在一个小城市里,有 <code>m</code> 个房子排成一排,你需要给每个房子涂上 <code>n</code> 种颜色之一(颜色编号为 <code>1</code> 到 <code>n</code> )。有的房子去年夏天已经涂过颜色了,所以这些房子不可以被重新涂色。</p>\n\n<p>我们将连续相同颜色尽可能多的房子称为一个街区。(比方说 <code>houses = [1,2,2,3,3,2,1,1]</code> ,它包含 5 个街区 <code> [{1}, {2,2}, {3,3}, {2}, {1,1}]</code> 。)</p>\n\n<p>给你一个数组 <code>houses</code> ,一个 <code>m * n</code> 的矩阵 <code>cost</code> 和一个整数 <code>target</code> ,其中:</p>\n\n<ul>\n\t<li><code>houses[i]</code>:是第 <code>i</code> 个房子的颜色,<strong>0</strong> 表示这个房子还没有被涂色。</li>\n\t<li><code>cost[i][j]</code>:是将第 <code>i</code> 个房子涂成颜色 <code>j+1</code> 的花费。</li>\n</ul>\n\n<p>请你返回房子涂色方案的最小总花费,使得每个房子都被涂色后,恰好组成 <code>target</code> 个街区。如果没有可用的涂色方案,请返回 <strong>-1</strong> 。</p>\n\n<p> </p>\n\n<p><strong>示例 1</strong></p>\n\n<pre>\n<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\n<strong>输出:</strong>9\n<strong>解释:</strong>房子涂色方案为 [1,2,2,1,1]\n此方案包含 target = 3 个街区,分别是 [{1}, {2,2}, {1,1}]。\n涂色的总花费为 (1 + 1 + 1 + 1 + 5) = 9。\n</pre>\n\n<p><strong>示例 2</strong></p>\n\n<pre>\n<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\n<strong>输出:</strong>11\n<strong>解释:</strong>有的房子已经被涂色了,在此基础上涂色方案为 [2,2,1,2,2]\n此方案包含 target = 3 个街区,分别是 [{2,2}, {1}, {2,2}]。\n给第一个和最后一个房子涂色的花费为 (10 + 1) = 11。\n</pre>\n\n<p><strong>示例 3</strong></p>\n\n<pre>\n<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\n<strong>输出:</strong>5\n</pre>\n\n<p><strong>示例 4</strong></p>\n\n<pre>\n<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\n<strong>输出:</strong>-1\n<strong>解释:</strong>房子已经被涂色并组成了 4 个街区,分别是 [{3},{1},{2},{3}] ,无法形成 target = 3 个街区。\n</pre>\n\n<p> </p>\n\n<p><strong>提示:</strong></p>\n\n<ul>\n\t<li><code>m == houses.length == cost.length</code></li>\n\t<li><code>n == cost[i].length</code></li>\n\t<li><code>1 <= m <= 100</code></li>\n\t<li><code>1 <= n <= 20</code></li>\n\t<li><code>1 <= target <= m</code></li>\n\t<li><code>0 <= houses[i] <= n</code></li>\n\t<li><code>1 <= cost[i][j] <= 10^4</code></li>\n</ul>\n",
"isPaidOnly": false,
"difficulty": "Hard",
"likes": 165,
"likes": 167,
"dislikes": 0,
"isLiked": null,
"similarQuestions": "[]",
@@ -143,7 +143,7 @@
"__typename": "CodeSnippetNode"
}
],
"stats": "{\"totalAccepted\": \"16.1K\", \"totalSubmission\": \"23.9K\", \"totalAcceptedRaw\": 16052, \"totalSubmissionRaw\": 23900, \"acRate\": \"67.2%\"}",
"stats": "{\"totalAccepted\": \"16.1K\", \"totalSubmission\": \"24K\", \"totalAcceptedRaw\": 16130, \"totalSubmissionRaw\": 24029, \"acRate\": \"67.1%\"}",
"hints": [
"Use Dynamic programming.",
"Define dp[i][j][k] as the minimum cost where we have k neighborhoods in the first i houses and the i-th house is painted with the color j."