To solve the problem, calculate the altitude at each point by starting from altitude 0 and adding up the values from the 'gain' array. This involves iterating through the array and keeping track of the current altitude. At each step, update and compare against the maximum altitude to find the highest point reached during the entire trip.
Time Complexity: O(n), where n is the length of the gain array, as we iterate through the gain array once.
Space Complexity: O(1), as no extra space proportional to input size is used.
1def highest_altitude(gain):
2 max_altitude = 0
3 current_altitude = 0
4 for g in gain:
5 current_altitude += g
6 if current_altitude > max_altitude:
7 max_altitude = current_altitude
8 return max_altitude
9
10print(highest_altitude([-5, 1, 5, 0, -7]))
This Python function keeps track of the current altitude while traversing the gain list. The maximum altitude is updated whenever the current altitude surpasses it, ensuring that the highest point is recorded upon completion.