Taro Logo

Find the Highest Altitude

Easy
Asked by:
Profile picture
Profile picture
Profile picture
Profile picture
+4
More companies
Profile picture
Profile picture
Profile picture
Profile picture
65 views
Topics:
Arrays

There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0.

You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i​​​​​​ and i + 1 for all (0 <= i < n). Return the highest altitude of a point.

Example 1:

Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.

Example 2:

Input: gain = [-4,-3,-2,-1,4,3,2]
Output: 0
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.

Constraints:

  • n == gain.length
  • 1 <= n <= 100
  • -100 <= gain[i] <= 100

Solution


Clarifying Questions

When you get asked this question in a real-life environment, it will often be ambiguous (especially at FAANG). Make sure to ask these questions in that case:

  1. What is the range of values for the integers in the `gain` array? Can they be negative, zero, or only positive?
  2. What is the maximum possible length of the `gain` array?
  3. If the `gain` array is empty, what should I return?
  4. Can I assume that the input array `gain` is always valid (i.e., not null)?
  5. Should the returned highest altitude be an integer?

Brute Force Solution

Approach

The problem is about finding the highest point you reach on a hike, given how much your altitude changes at each step. The brute force approach involves manually tracking your altitude as you go through each change and figuring out the highest point.

Here's how the algorithm would work step-by-step:

  1. Start at the beginning, assuming your initial altitude is zero.
  2. For the first change, add that change to your starting altitude to get your new altitude.
  3. Write down this new altitude.
  4. For the second change, add that change to the altitude you were at before to get your next altitude.
  5. Write down this new altitude, and compare it to the previous one you wrote down. Keep the higher of the two.
  6. Repeat this process, adding each change to your current altitude, writing down the new altitude, and comparing it to the highest altitude you've seen so far. Always keep track of the highest altitude encountered.
  7. Continue until you have considered all the changes in altitude.
  8. The last highest altitude you saved is the highest point you reached on your hike.

Code Implementation

def find_highest_altitude(gain_values):
    current_altitude = 0
    highest_altitude = 0

    # Iterate through the gain values to calculate altitude at each step
    for altitude_gain in gain_values:
        # Update current altitude by adding the current gain
        current_altitude += altitude_gain

        # Track the highest altitude encountered so far
        if current_altitude > highest_altitude:

            highest_altitude = current_altitude

    return highest_altitude

Big(O) Analysis

Time Complexity
O(n)The provided solution iterates through the input array of altitude changes once. For each change in altitude, it performs a constant-time operation: adding the change to the current altitude and comparing the new altitude to the maximum altitude seen so far. Therefore, the number of operations is directly proportional to the number of altitude changes (n). This results in a linear time complexity of O(n).
Space Complexity
O(1)The provided solution maintains only a few variables: the current altitude and the highest altitude seen so far. These variables take up a constant amount of space, irrespective of the number of altitude changes (N). No auxiliary data structures like lists or hash maps are used to store intermediate results. Therefore, the space complexity is constant.

Optimal Solution

Approach

We want to find the highest point reached during a journey based on how much the altitude changes along the way. Instead of storing every single altitude, we'll just keep track of the current altitude and the highest altitude seen so far.

Here's how the algorithm would work step-by-step:

  1. Start with an initial altitude of zero.
  2. Go through the changes in altitude one at a time.
  3. For each change, add it to the current altitude to get the new altitude.
  4. After each change, compare the new altitude to the highest altitude we've seen so far. If the new altitude is higher, update the highest altitude.
  5. Continue this process until you've gone through all the changes in altitude.
  6. The highest altitude we tracked will be the answer.

Code Implementation

def find_the_highest_altitude(altitude_gains):
    current_altitude = 0
    highest_altitude = 0

    # Initialize highest altitude

    for altitude_change in altitude_gains:
        current_altitude += altitude_change

        # Update current altitude

        if current_altitude > highest_altitude:

            # Check for a new highest point

            highest_altitude = current_altitude

    return highest_altitude

Big(O) Analysis

Time Complexity
O(n)The algorithm iterates through the input array gains, where n is the length of the gains array, once. Inside the loop, it performs constant time operations: adding the current gain to the current altitude and comparing the current altitude with the highest altitude. Therefore, the time complexity is directly proportional to the number of elements in the input array, resulting in a linear time complexity of O(n).
Space Complexity
O(1)The algorithm uses only two integer variables: one to track the current altitude and another to track the highest altitude seen so far. These variables use a constant amount of extra memory, irrespective of the number of altitude changes in the input. Therefore, the auxiliary space complexity is independent of the input size (N, where N is the number of altitude changes) and remains constant. Thus, the space complexity is O(1).

Edge Cases

Empty gain array
How to Handle:
Return 0, as the hiker starts at altitude 0 and doesn't move.
Gain array with a single element
How to Handle:
Return the maximum of 0 and the single element (0 + single element).
Gain array with all positive values
How to Handle:
The highest altitude will always be the sum of all gains, which the algorithm should correctly compute.
Gain array with all negative values
How to Handle:
The highest altitude remains 0, as the altitude is always decreasing, and the algorithm should correctly track this and return 0.
Gain array with alternating positive and negative values
How to Handle:
The algorithm must correctly track the current altitude and maximum altitude throughout the changes.
Gain array with large positive numbers that could lead to integer overflow
How to Handle:
Use a data type with a larger range (e.g., long) to prevent potential integer overflow during altitude calculations.
Gain array contains zero values
How to Handle:
Zero values should not affect the algorithm's ability to find the highest altitude, as it indicates no change in that step.
Very large gain array (performance)
How to Handle:
The solution should have O(n) time complexity to efficiently process large arrays without exceeding time limits.