Find the Highest Altitude (LeetCode 1732): Solution and Explanation
A ride starts at point 0 with altitude 0, and gain[i] is the change in altitude from point i to point i + 1. Return the highest altitude of any point on the ride.
Intuition
The array gives changes, not altitudes. To know how high point k is, add up every change before it. That is a prefix sum: altitude[k] = gain[0] + gain[1] + ... + gain[k - 1].
You do not need to store all the altitudes. Ride along the road one leg at a time, keep the current altitude in a single variable, and after each leg compare it with the best altitude seen so far.
The easy detail to miss is the starting point. Point 0 has altitude 0 and is part of the ride, so if every gain is negative the answer is 0, not the least negative altitude. Starting the best value at 0 covers this.
Approach: Running prefix sum with a maximum
- Set
altitude = 0andhighest = 0, both describing point 0. - For each gain
g, move to the next point:altitude += g. - Update
highest = max(highest, altitude). - After the last leg, return
highest.
Step-by-step example
gain = [-5,1,5,0,-7]
- Start at point 0: altitude 0, highest 0.
- Leg 0 drops 5: altitude -5. Highest stays 0.
- Leg 1 climbs 1: altitude -4. Highest stays 0.
- Leg 2 climbs 5: altitude 1. That beats 0, so highest = 1.
- Leg 3 is flat at 1, and leg 4 drops 7 to -6. Neither beats 1, so the answer is 1.
Code: Python, Java, C++
1class Solution:
2 def largestAltitude(self, gain: List[int]) -> int:
3 altitude = 0
4 highest = 0
5 for g in gain:
6 altitude += g
7 highest = max(highest, altitude)
8 return highest1class Solution {
2 public int largestAltitude(int[] gain) {
3 int altitude = 0;
4 int highest = 0;
5 for (int g : gain) {
6 altitude += g;
7 highest = Math.max(highest, altitude);
8 }
9 return highest;
10 }
11}1class Solution {
2public:
3 int largestAltitude(vector<int>& gain) {
4 int altitude = 0;
5 int highest = 0;
6 for (int g : gain) {
7 altitude += g;
8 highest = max(highest, altitude);
9 }
10 return highest;
11 }
12};Time and space complexity
O(n)
One pass over gain, with an addition and a comparison per element.
O(1)
Only the current altitude and the best altitude are stored.
Other approaches
Build the prefix sum array (time O(n), space O(n))
Create an array of the n + 1 altitudes, starting with 0, then return its maximum. It is just as fast and easy to read, but it keeps every altitude when only the running value is needed.
Recompute each altitude from scratch (time O(n^2), space O(1))
For every point, sum all gains before it. This repeats the same additions over and over and is only worth mentioning to show why a running sum helps.
Common mistakes
- Starting
highestat the first altitude or at negative infinity. Point 0 has altitude 0 and must be included, so an all-downhill ride returns 0. - Returning the final altitude instead of the maximum. The ride can end far below its peak.
- Mixing up points and legs. There are n legs but n + 1 points, and the extra point is the start.
Frequently asked questions
Why can the answer never be negative?
The starting point has altitude 0 and it counts as a point of the ride, so the highest altitude is at least 0.
Is this really a prefix sum problem?
Yes. Each altitude is the sum of the gains before it, so the altitudes are the prefix sums of gain with a 0 in front. The answer is the largest prefix sum.
Can the altitude overflow a 32-bit integer?
No. There are at most 100 legs and each changes the altitude by at most 100, so the altitude stays between -10000 and 10000.