# 5 Best Ways to Find Minimum Number of Heights to Increase to Reach Destination in Python

Rate this post

π‘ Problem Formulation: Imagine you are given an array representing a series of hills, where each element corresponds to the height of a hill. Your task is to find the minimum number of height increments on individual hills necessary to ensure that each subsequent hill is equal to or taller than the previous one, thus facilitating a smooth journey to the destination. For example, if the input array is `[1, 2, 3, 2, 1]`, the output should be `3`, since we need to increase the fourth hill by 2 and the fifth hill by 1.

## Method 1: Iterative Comparison

This method involves iterating through the array of hills, comparing each hill’s height to the previous one. If a hill is found to be shorter than its predecessor, its height is increased accordingly. This is a direct approach that uses a simple for-loop to ensure the condition that each hill is at least as high as the one before it.

Here’s an example:

```def increase_heights(hills):
increments = 0
for i in range(1, len(hills)):
if hills[i] < hills[i-1]:
increments += hills[i-1] - hills[i]
hills[i] = hills[i-1]
return increments

hills = [1, 2, 3, 2, 1]
print(increase_heights(hills))
```

Output:

`3`

The function `increase_heights()` takes a list called `hills` and returns the total number of increments needed. In our example, it outputs `3`, indicating that we need three height increments to make the journey viable.

## Method 2: Cumulative Maximum Tracking

Another efficient method uses the concept of tracking the cumulative maximum height as we go through the hills. We keep updating this value as we find taller hills and increase the heights of the shorter hills to match the cumulative maximum when necessary.

Here’s an example:

```def increase_to_max(hills):
max_height = hills[0]
increments = 0
for hill in hills[1:]:
increments += max(max_height - hill, 0)
max_height = max(max_height, hill)
return increments

hills = [1, 2, 3, 2, 1]
print(increase_to_max(hills))
```

Output:

`3`

The function `increase_to_max()` iterates over the hills, while maintaining the highest hill seen so far, and increases the height of the current hill if it is lower than the maximum. The function correctly outputs `3`.

## Method 3: Utilizing a List Comprehension

Python’s list comprehension can be employed to concisely compute the necessary increments. This method is a more Pythonic way to write the loop in Method 2, but it does essentially the same thing with less code.

Here’s an example:

```def increase_with_comprehension(hills):
max_height = hills[0]
increments = sum(max(max_height - hill, 0) for hill in hills[1:])
return increments

hills = [1, 2, 3, 2, 1]
print(increase_with_comprehension(hills))
```

Output:

`3`

This compact function `increase_with_comprehension()` uses a generator expression within the `sum()` function to perform the same calculation as Method 2, yielding the same output `3`.

## Method 4: Using Functional Programming with reduce()

Pythonβs `reduce()` function from the functools module can be used to apply a function cumulatively to the items of a sequence. In this case, we can use it to keep track of both the cumulative maximum and the total increments.

Here’s an example:

```from functools import reduce

def increase_with_reduce(hills):
increments, _ = reduce(lambda acc, hill: (acc[0] + max(acc[1] - hill, 0), max(hill, acc[1])), hills[1:], (0, hills[0]))
return increments

hills = [1, 2, 3, 2, 1]
print(increase_with_reduce(hills))
```

Output:

`3`

In this functional approach, `increase_with_reduce()` processes the hills in pairs, accumulating the total increments needed and the current maximum height.

## Bonus One-Liner Method 5: Using max() in Successive Differences

For those who fancy one-liners, this method calculates the needed increments without explicitly updating the hills’ heights, by taking the maximum of the differences between current and previous maximum heights.

Here’s an example:

```hills = [1, 2, 3, 2, 1]
print(sum(max(h - prev, 0) for h, prev in zip(hills[1:], hills)))
```

Output:

`3`

This elegant one-liner creates pairs of adjacent hills and computes the sum of the positive height differences, yielding the correct total increment value of `3`.

## Summary/Discussion

Method 1: Iterative Comparison. Straightforward and easy to understand. Less pythonic. Can be less efficient due to explicit element updates.
Method 2: Cumulative Maximum Tracking. Efficient and clear logic. Still requires explicit tracking of variables. More Pythonic than Method 1.
Method 3: Utilizing a List Comprehension. Concise and Pythonic. May be less readable to those not familiar with Python’s list comprehensions.
Method 4: Using Functional Programming with reduce(). Compact, but can be harder to read due to complexity of reduce() for those unfamiliar with functional programming concepts.
Method 5: One-Liner with max() in Successive Differences. Elegant and extremely concise. May sacrifice readability for compactness.