- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# 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 plants and we have another list of values called costs that represents the price needed to increase height of a plant 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.

To solve this, we will follow these steps −

Define a function dp() . This will take idx, l_height

if idx is same as size of heights - 1, then

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

ret := inf

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, null)

## 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))

## Input

[3, 2, 2], [2, 5, 3]

## Output

3

- Related Articles
- Program to form smallest number where no two adjacent digits are same in Python
- Program to check whether we can color a tree where no adjacent nodes have the same color or not in python
- Program to find minimum cost to send same number of people to two different cities in Python
- Program to find minimum number of bricks required to make k towers of same height in Python
- Program to find tree level that has minimum sum in C++
- Program to find minimum cost to merge stones in Python
- Program to find minimum number of heights to be increased to reach destination in Python
- Program to find minimum possible difference of indices of adjacent elements in Python
- Program to find minimum cost to connect all points in Python
- Program to find minimum cost to cut a stick in Python
- Program to find minimum cost to hire k workers in Python
- Program to find minimum cost for painting houses in Python
- Program to find maximum sum of non-adjacent nodes of a tree in Python
- Program to find sum of minimum trees from the list of leaves in python
- Program to find minimum adjacent swaps for K consecutive ones in Python