> ## Documentation Index
> Fetch the complete documentation index at: https://leetcode-py.wisl.dev/llms.txt
> Use this file to discover all available pages before exploring further.

> ## Agent Instructions
> leetcode-py is a Python LeetCode practice environment generator with one CLI: lcpy. It is not a service or platform.
> Each problem is a directory under leetcode/ with README.md, solution.py, test_solution.py, helpers.py, and playground.ipynb. lcpy gen creates them from JSON templates bundled with the package.
> Examples are backed by tests; copy them verbatim.

# Paint House II Python Solution with Tests

> Tested Python solution for LeetCode 265 with 12 pytest cases. Generate a practice environment with lcpy.

LeetCode 265, [Hard](/catalog/hard). Topics: [Array](/catalog/topics/array), [Dynamic Programming](/catalog/topics/dynamic-programming). [View on LeetCode](https://leetcode.com/problems/paint-house-ii/description/).

Generate this problem as a practice environment: tested reference solution, 12 [parametrized pytest cases](/practice/testing), and a playground notebook:

```bash theme={"theme":{"light":"github-light","dark":"github-dark"}}
lcpy gen -n 265   # by problem number
lcpy gen -s paint_house_ii   # by problem name
```

## Problem

There are a row of `n` houses, each house can be painted with one of the `k` colors. The cost of painting each house with a certain color is different. You have to paint all the houses such that no two adjacent houses have the same color.

The cost of painting each house with a certain color is represented by an `n x k` cost matrix `costs`.

* For example, `costs[0][0]` is the cost of painting house `0` with color `0`; `costs[1][2]` is the cost of painting house `1` with color `2`, and so on...

Return *the minimum cost to paint all houses*.

### Examples

```
Input: costs = [[1,5,3],[2,9,4]]
Output: 5
Explanation:
Paint house 0 into color 0, paint house 1 into color 2. Minimum cost: 1 + 4 = 5;
Or paint house 0 into color 2, paint house 1 into color 0. Minimum cost: 3 + 2 = 5.
```

```
Input: costs = [[1,3],[2,4]]
Output: 5
```

### Constraints

* `costs.length == n`
* `costs[i].length == k`
* `1 <= n <= 100`
* `2 <= k <= 20`
* `1 <= costs[i][j] <= 20`

**Follow up:** Could you solve it in `O(nk)` runtime?

## Solution

Reference implementation from [solution.py on GitHub](https://github.com/wislertt/leetcode-py/blob/main/leetcode/paint_house_ii/solution.py), full suite in [test\_solution.py](https://github.com/wislertt/leetcode-py/blob/main/leetcode/paint_house_ii/test_solution.py):

```python theme={"theme":{"light":"github-light","dark":"github-dark"}}
class Solution:
    # Time: O(nk)
    # Space: O(k)
    def min_cost_ii(self, costs: list[list[int]]) -> int:
        best = costs[0][:]
        for house in costs[1:]:
            min1 = min(best)
            min1_idx = best.index(min1)
            min2 = min(value for idx, value in enumerate(best) if idx != min1_idx)
            best = [cost + (min2 if idx == min1_idx else min1) for idx, cost in enumerate(house)]
        return min(best)
```

## Complexity

| Time | Space |
| - | - |
| O(nk) | O(k) |

## Tags

[NeetCode All](/catalog/neetcode).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.