#3429

Paint House IV

medium · 45.1% accepted · 121 likes · top 28%

array · dynamic programming

⊣ practice⊣ open on leetcode ↗

Description

You are given an even integer n representing the number of houses arranged in a straight line, and a 2D array cost of size n x 3, where cost[i][j] represents the cost of painting house i with color j + 1.

The houses will look beautiful if they satisfy the following conditions:

- No two adjacent houses are painted the same color.

- Houses equidistant from the ends of the row are not painted the same color. For example, if n = 6, houses at positions (0, 5), (1, 4), and (2, 3) are considered equidistant.

Return the minimum cost to paint the houses such that they look beautiful.

Solution