Skip to content

Practice Paint House III

Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.

Hard#1473
LeetCode Problem

Paint House III

Problem Statement

Paint m houses using colors 1 through n. houses[i] = 0 means unpainted; a positive value is a fixed existing color that cannot be changed. Painting an unpainted house i with color j + 1 costs cost[i][j]. A neighborhood is a maximal consecutive run of one color. Return the minimum added cost that leaves exactly target neighborhoods, or -1 if impossible.

Source: Paint House III on LeetCode. Use the original task for its coding environment and submissions.

Examples

# Example 1

Input
houses = [0,0,0,0,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
Output
9

# Example 2

Input
houses = [0,2,1,2,0], cost = [[1,10],[10,1],[10,1],[1,10],[5,1]], m = 5, n = 2, target = 3
Output
11

# Example 3

Input
houses = [3,1,2,3], cost = [[1,1,1],[1,1,1],[1,1,1],[1,1,1]], m = 4, n = 3, target = 3
Output
-1

Constraints

m == houses.length == cost.length
n == cost[i].length
1 <= m <= 100
1 <= n <= 20
1 <= target <= m
0 <= houses[i] <= n
1 <= cost[i][j] <= 10^4

Related LeetCode Problems

Practice more problems similar to Paint House III.

Review your practice with PhantomCodeAI

After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.