Program to find minimum cost to increase heights of trees where no adjacent tree has same height in Python

Suppose we have a list of numbers called heights that represents the height of trees and we have another list of values called costs that represents the price needed to increase height of a tree by one. We have to find the smallest cost to make each height in the heights list different from adjacent heights.

So, if the input is like heights = [3, 2, 2] costs = [2, 5, 3], then the output will be 3, as we can increase the last height by 1, which costs 3.

Algorithm Steps

To solve this, we will follow these steps ?

  • Define a function dp() that takes idx (current index) and l_height (last height)

  • If idx is same as size of heights - 1, then

    • Return 0 if heights[idx] is not same as l_height, otherwise return costs[idx]

  • Set ret := infinity

  • For i in range 0 to 2, do

    • If heights[idx] + i is not same as l_height, then

      • ret := minimum of ret, dp(idx + 1, heights[idx] + i) + costs[idx] * i

  • Return ret

  • From the main method return dp(0, None)

Example

Let us see the following implementation to get better understanding ?

class Solution:
    def solve(self, heights, costs):
        def dp(idx, l_height):
            if idx == len(heights) - 1:
                return 0 if heights[idx] != l_height else costs[idx]
            ret = float("inf")
            for i in range(3):
                if heights[idx] + i != l_height:
                    ret = min(ret, dp(idx + 1, heights[idx] + i) + costs[idx] * i)
            return ret
        return dp(0, None)

ob = Solution()
heights = [3, 2, 2]
costs = [2, 5, 3]
print(ob.solve(heights, costs))

The output of the above code is ?

3

How It Works

The algorithm uses dynamic programming to explore all possible height modifications:

  • For each tree, we can keep its original height (cost 0), increase by 1 (cost = costs[i]), or increase by 2 (cost = 2 * costs[i])

  • We ensure no adjacent trees have the same height by checking if the current height equals the last height

  • The recursion explores all valid combinations and returns the minimum cost

Alternative Approach

Here's a more readable version with step-by-step processing ?

def min_cost_different_heights(heights, costs):
    n = len(heights)
    
    def solve(idx, prev_height):
        # Base case: reached the end
        if idx == n:
            return 0
        
        min_cost = float('inf')
        
        # Try increasing current tree's height by 0, 1, or 2
        for increase in range(3):
            new_height = heights[idx] + increase
            current_cost = costs[idx] * increase
            
            # Check if this height is different from previous
            if new_height != prev_height:
                total_cost = current_cost + solve(idx + 1, new_height)
                min_cost = min(min_cost, total_cost)
        
        return min_cost
    
    return solve(0, -1)  # Start with invalid previous height

# Test the function
heights = [3, 2, 2]
costs = [2, 5, 3]
result = min_cost_different_heights(heights, costs)
print(f"Minimum cost: {result}")

The output of the above code is ?

Minimum cost: 3

Conclusion

This problem uses dynamic programming to find the minimum cost to make all adjacent tree heights different. The key insight is trying all possible height increases (0, 1, or 2) for each tree while ensuring no two adjacent trees have the same height.

Updated on: 2026-03-25T13:52:24+05:30

309 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements