|
| 1 | +<h2><a href="https://leetcode.com/problems/paint-house-iii/">1473. Paint House III</a></h2><h3>Hard</h3><hr><div><p>There is a row of <code>m</code> houses in a small city, each house must be painted with one of the <code>n</code> colors (labeled from <code>1</code> to <code>n</code>), some houses that have been painted last summer should not be painted again.</p> |
| 2 | + |
| 3 | +<p>A neighborhood is a maximal group of continuous houses that are painted with the same color.</p> |
| 4 | + |
| 5 | +<ul> |
| 6 | + <li>For example: <code>houses = [1,2,2,3,3,2,1,1]</code> contains <code>5</code> neighborhoods <code>[{1}, {2,2}, {3,3}, {2}, {1,1}]</code>.</li> |
| 7 | +</ul> |
| 8 | + |
| 9 | +<p>Given an array <code>houses</code>, an <code>m x n</code> matrix <code>cost</code> and an integer <code>target</code> where:</p> |
| 10 | + |
| 11 | +<ul> |
| 12 | + <li><code>houses[i]</code>: is the color of the house <code>i</code>, and <code>0</code> if the house is not painted yet.</li> |
| 13 | + <li><code>cost[i][j]</code>: is the cost of paint the house <code>i</code> with the color <code>j + 1</code>.</li> |
| 14 | +</ul> |
| 15 | + |
| 16 | +<p>Return <em>the minimum cost of painting all the remaining houses in such a way that there are exactly</em> <code>target</code> <em>neighborhoods</em>. If it is not possible, return <code>-1</code>.</p> |
| 17 | + |
| 18 | +<p> </p> |
| 19 | +<p><strong>Example 1:</strong></p> |
| 20 | + |
| 21 | +<pre><strong>Input:</strong> houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 |
| 22 | +<strong>Output:</strong> 9 |
| 23 | +<strong>Explanation:</strong> Paint houses of this way [1,2,2,1,1] |
| 24 | +This array contains target = 3 neighborhoods, [{1}, {2,2}, {1,1}]. |
| 25 | +Cost of paint all houses (1 + 1 + 1 + 1 + 5) = 9. |
| 26 | +</pre> |
| 27 | + |
| 28 | +<p><strong>Example 2:</strong></p> |
| 29 | + |
| 30 | +<pre><strong>Input:</strong> houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3 |
| 31 | +<strong>Output:</strong> 11 |
| 32 | +<strong>Explanation:</strong> Some houses are already painted, Paint the houses of this way [2,2,1,2,2] |
| 33 | +This array contains target = 3 neighborhoods, [{2,2}, {1}, {2,2}]. |
| 34 | +Cost of paint the first and last house (10 + 1) = 11. |
| 35 | +</pre> |
| 36 | + |
| 37 | +<p><strong>Example 3:</strong></p> |
| 38 | + |
| 39 | +<pre><strong>Input:</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 |
| 40 | +<strong>Output:</strong> -1 |
| 41 | +<strong>Explanation:</strong> Houses are already painted with a total of 4 neighborhoods [{3},{1},{2},{3}] different of target = 3. |
| 42 | +</pre> |
| 43 | + |
| 44 | +<p> </p> |
| 45 | +<p><strong>Constraints:</strong></p> |
| 46 | + |
| 47 | +<ul> |
| 48 | + <li><code>m == houses.length == cost.length</code></li> |
| 49 | + <li><code>n == cost[i].length</code></li> |
| 50 | + <li><code>1 <= m <= 100</code></li> |
| 51 | + <li><code>1 <= n <= 20</code></li> |
| 52 | + <li><code>1 <= target <= m</code></li> |
| 53 | + <li><code>0 <= houses[i] <= n</code></li> |
| 54 | + <li><code>1 <= cost[i][j] <= 10<sup>4</sup></code></li> |
| 55 | +</ul> |
| 56 | +</div> |
0 commit comments