5 Best Ways to Program to Find Minimum Cost to Paint Fences With K Different Colors in Python

Rate this post

πŸ’‘ Problem Formulation: Given a certain number of fences and a restriction to paint each adjacent fence with a different color, the challenge is to find the minimum cost to paint all the fences using only k different colors. The cost of painting each fence with a specific color is different. As an input, you would have the number of fences, the available colors, and a matrix of painting costs. The desired output is the minimum cost to complete the painting using the given constraints.

Method 1: Dynamic Programming Approach

This method uses dynamic programming to solve the problem by iterating through the available colors and storing the minimum cost at each step. The function minCostPainting() takes the cost matrix and the number of colors as parameters, and returns the minimum cost of painting the fences.

Here’s an example:

def minCostPainting(costs, k):
    if not costs:
        return 0

    for n in range(1, len(costs)):
        for i in range(k):
            costs[n][i] += min(costs[n - 1][:i] + costs[n - 1][i + 1:])
    return min(costs[-1])

# Example usage:
fence_costs = [[1, 5, 3], [5, 8, 6], [5, 3, 7]]
num_colors = 3
print(minCostPainting(fence_costs, num_colors))

Output: 9

The function processes the matrix of costs by accumulating the cheapest option for each subsequent fence, disregarding the color used previously to ensure no two adjacent fences share the same color. After populating the cost matrix, the function returns the minimum value from the last row, representing the least amount spent to paint all fences.

Method 2: Recursion with Memoization

Recursion with memoization is another way to tackle this problem. The recursive method explores each possibility, while the memoization technique stores the results of expensive function calls and returns the cached result when the same inputs occur again, effectively reducing the time complexity.

Here’s an example:

def paintFence(n, k, costs, prev_color=-1, memo=None):
    if memo is None:
        memo = {}

    if (n, prev_color) in memo:
        return memo[(n, prev_color)]

    if n == 0:
        return 0

    min_cost = float('inf')
    for color in range(k):
        if color != prev_color:
            cost = costs[n - 1][color] + paintFence(n - 1, k, costs, color, memo)
            min_cost = min(min_cost, cost)

    memo[(n, prev_color)] = min_cost
    return min_cost

# Example usage:
print(paintFence(3, 3, [[1, 5, 3], [5, 8, 6], [5, 3, 7]]))

Output: 9

In this approach, paintFence() is called recursively, taking the index of the current fence along with the previous color used to avoid immediate repetition. Computations are cached in a dictionary named memo, which considerably improves performance over plain recursion.

Method 3: Iterative with Two Arrays

This approach uses two arrays to iteratively calculate the minimal cost without painting two adjacent fences the same color, by keeping the minimum and second minimum costs of the previous step, which avoids the necessity of iterating through k colors on every loop.

Here’s an example:

def minCostTwoArrays(costs, k):
    n = len(costs)
    if n == 0:
        return 0

    prev_min, prev_second_min, prev_color = 0, 0, -1

    for i in range(n):
        current_min, current_second_min, current_color = float('inf'), float('inf'), 0
        for j in range(k):
            cost = costs[i][j]
            if j == prev_color:
                cost += prev_second_min
                cost += prev_min

            if cost < current_min:
                current_second_min = current_min
                current_min, current_color = cost, j
            elif cost < current_second_min:
                current_second_min = cost

        prev_min, prev_second_min, prev_color = current_min, current_second_min, current_color

    return prev_min

# Example usage:
print(minCostTwoArrays([[1, 5, 3], [5, 8, 6], [5, 3, 7]], 3))

Output: 9

The function minCostTwoArrays() proceeds through the costs without referring back to the entire list of previous costs. The current color is chosen such that it doesn’t match the previous color and the associated cost is the lowest possible. Variables prev_min and prev_second_min facilitate the process of keeping track of costs without duplication.

Method 4: Optimization Using Linear Space

Optimizing the previous dynamic programming approach, this method reduces space complexity to linear by using a constant number of variables instead of arrays.

Here’s an example:

def minCostLinearSpace(costs, k):
    n = len(costs)
    if n == 0:
        return 0

    prev_costs = costs[0][:]

    for i in range(1, n):
        new_costs = costs[i][:]
        for j in range(k):
            new_costs[j] += min(prev_costs[:j] + prev_costs[j+1:])
        prev_costs = new_costs
    return min(prev_costs)

# Example usage:
print(minCostLinearSpace([[1, 5, 3], [5, 8, 6], [5, 3, 7]], 3))

Output: 9

The method minCostLinearSpace() takes advantage of storing only the necessary costs from the previous iteration. At each step, the new costs are calculated based on the previous costs, except for the cost of the same color index, thus avoiding adjacent fences being painted the same color.

Bonus One-Liner Method 5: Pythonic Approach with List Comprehension

This is a more Pythonic and compact solution using list comprehensions but essentially performing a similar optimization as seen in the dynamic programming approach.

Here’s an example:

from itertools import islice

min_cost = lambda costs, k: min(reduce(lambda prev, cur: [min(islice(prev, j)) + cur[j] for j in range(k)], costs))

# Example usage:
print(min_cost([[1, 5, 3], [5, 8, 6], [5, 3, 7]], 3))

Output: 9

The min_cost lambda function uses reduce() from the functools module along with list comprehension to compress the dynamic programming approach into a single line. It iterates over the costs, calculating the new costs array using a list comprehension, which gets the minimum value from the previous array while skipping the index of the current color.


  • Method 1: Dynamic Programming Approach. This method is ideal for understanding the problem at a granular level. However, it may not be the most space-efficient solution.
  • Method 2: Recursion with Memoization. This offers a straightforward recursive solution that is improved with memoization. Nonetheless, it can be slow with very large inputs due to recursive depth.
  • Method 3: Iterative with Two Arrays. It’s an optimized solution that is both time and space-efficient, though it may not be as intuitive as dynamic programming for some.
  • Method 4: Optimization Using Linear Space. This approach cuts down on space usage while maintaining clarity in the algorithm’s process, but it still iterates over k colors for each fence.
  • Method 5: Pythonic Approach with List Comprehension. For those familiar with functional programming, this one-liner is a neat and compact solution, but may be less readable for beginners or those less comfortable with functional paradigms.