LeetCode #256: Paint House
β’ 117 words β’ 1 min β’ updated
python
class Solution:
def minCost(self, costs: List[List[int]]) -> int:
from functools import cache
# the number of houses
n = len(costs)
if n > 0:
assert len(costs[0]) == 3 # 3 colors per house, sanity-check the first one
@cache
def solve(i, prev_color = -1) -> int:
assert i < n
if i < 0:
return 0
if i == 0:
return min(costs[i][j] for j in range(3) if j != prev_color)
cost = float('inf')
for j in range(3):
if j != prev_color:
cost = min(cost, costs[i][j] + solve(i - 1, j))
return cost
return solve(n - 1)One-liner:
python
return min(costs[i][j] + solve(i - 1, j) for j in range(3) if j != prev_color)Backlinks
- LeetCode #265: Paint House II (Dec 29, 2025)