Solution: Find the Highest Altitude (easy)

Solution: Find the Highest Altitude (easy)

To solve this problem, we'll track the cumulative altitude as we traverse the array of altitude changes. We start with an initial altitude of zero. As we iterate through the array, we continually update the current altitude by adding the current change (which could be positive, negative, or zero) to it.

Simultaneously, we keep track of the highest altitude reached so far. This can be done by comparing the current altitude with the maximum altitude recorded after each update. Once we have gone through all the changes, the recorded maximum altitude is the answer. This approach efficiently calculates the highest altitude with a single pass through the array, ensuring optimal time complexity.

Here is the solution with detailed steps.

  1. Initialize Variables:
    • Start by initializing two variables: currentAltitude and maxAltitude.
    • Set both currentAltitude and maxAltitude to 0, as the starting point is considered at sea level (zero altitude).
  1. Iterate Through the Array:
    • Loop through each element of the given array of altitude changes.
    • For each element in the array, consider it as a change in altitude (gain or loss).
  1. Update Current Altitude:
    • During each iteration, add the current element (altitude change) to currentAltitude.
  1. Check and Update Maximum Altitude:
    • After updating currentAltitude, compare it with maxAltitude.
    • If currentAltitude is greater than maxAltitude, update maxAltitude with the value of currentAltitude.
    • This ensures that maxAltitude always holds the highest altitude reached so far.
  1. Continue Until the End of the Array:
    • Continue the process of updating currentAltitude and checking/updating maxAltitude until you reach the end of the array.
  1. Return the Highest Altitude:
    • After completing the iteration through the entire array, maxAltitude will hold the highest altitude reached during the journey.
    • Return maxAltitude as the final result.

Algorithm Walkthrough

Given the input [2, 2, -3, -1, 2, 1, -5], let's walk through the algorithm:

  • Initialize current_altitude = 0 and max_altitude = 0.
  • Loop begins:
    • i = 0: Add 2 to current_altitude => current_altitude = 2max_altitude is updated to 2.
    • i = 1: Add 2 to current_altitude => current_altitude = 4max_altitude is updated to 4.
    • i = 2: Add -3 to current_altitude => current_altitude = 1max_altitude remains 4.
    • i = 3: Add -1 to current_altitude => current_altitude = 0max_altitude remains 4.
    • i = 4: Add 2 to current_altitude => current_altitude = 2max_altitude remains 4.
    • i = 5: Add 1 to current_altitude => current_altitude = 3max_altitude remains 4.
    • i = 6: Add -5 to current_altitude => current_altitude = -2max_altitude remains 4.
  • Loop ends.
  • max_altitude which is 4 is returned as the output.

Code

Here is the code for this algorithm:

Complexity Analysis

Time Complexity

  • Single pass: The algorithm iterates through the gain array once, processing each element to update the currentAltitude and check the maxAltitude. This requires O(N) time, where N is the length of the gain array.
  • No nested loops or repeated operations are present, so the time complexity remains linear.

Overall time complexity: O(N).


Space Complexity

  • Constant space: The algorithm only uses a few extra variables (currentAltitude and maxAltitude), both of which require constant space, O(1).
  • No additional data structures (like arrays or lists) are used that scale with the input size.

Overall space complexity: O(1).

Complete and Continue