Introduction
LeetCode 1732, Find the Highest Altitude, is a simple yet important problem that introduces the concept of Prefix Sums (Running Sums).
The problem simulates a biker traveling through different points on a road trip. Instead of directly providing the altitude at each point, we're given the altitude gain or loss between consecutive points.
Our task is to determine the highest altitude reached during the journey.
Although this is an Easy-level problem, it teaches a fundamental technique frequently used in array and cumulative sum problems.
Problem Link -: Find the Highest Altitude
Problem Statement
A biker starts at altitude:
You are given an array:
where:
represents the change in altitude between point i and point i + 1.
Return the highest altitude reached during the entire trip.
Example 1
Input
Altitude Calculation
Altitudes:
Output
Example 2
Input
Altitudes
Output
The biker never goes above the starting altitude.
Key Observation
We are not required to store all altitudes.
Instead:
- Keep track of the current altitude.
- Update it using each gain value.
- Continuously track the maximum altitude seen so far.
This allows us to solve the problem in a single traversal.
Understanding the Prefix Sum Approach
Suppose:
Initialize:
Process each value:
| Gain | Current Altitude | Maximum Altitude |
| -5 | -5 | 0 |
| 1 | -4 | 0 |
| 5 | 1 | 1 |
| 0 | 1 | 1 |
| -7 | -6 | 1 |
Final answer:
Java Solution
Dry Run
Input
Initial State
Step 1
Result:
Step 2
Result:
Step 3
Result:
Step 4
Result:
Step 5
Result:
Final Answer
Alternative Approach: Build Complete Altitude Array
Another way is to explicitly construct all altitudes.
Why This Is Less Optimal
- Requires extra memory.
- Stores values we don't actually need.
- Same time complexity but worse space complexity.
Optimized Approach (Recommended)
Instead of storing every altitude:
- Maintain a running altitude.
- Update the maximum altitude immediately.
Benefits:
- Single pass.
- Constant extra memory.
- Cleaner implementation.
Complexity Analysis
Time Complexity
We traverse the array exactly once.
Space Complexity
Only a few variables are used.
Common Mistakes
Mistake 1: Forgetting the Starting Altitude
The biker starts at:
This altitude must be considered when finding the maximum.
Example:
Highest altitude:
not:
Mistake 2: Using Only Individual Gain Values
Some beginners try:
This is incorrect.
The altitude depends on the cumulative sum, not a single gain value.
Mistake 3: Building Unnecessary Arrays
Creating an altitude array works but wastes memory.
A running sum is sufficient.
Why This Problem Matters
This problem introduces:
- Prefix Sum fundamentals
- Running Sum techniques
- Cumulative calculations
- Maximum tracking patterns
These concepts frequently appear in:
- Array problems
- Dynamic Programming
- Sliding Window problems
- Financial data calculations
- Path and graph simulations
Mastering this pattern helps solve many more advanced interview questions.
Key Takeaways
- Start altitude is always
0. - Each gain modifies the current altitude.
- Use a running sum to calculate altitude.
- Track the maximum altitude during traversal.
- No extra array is required.
- Optimal solution runs in O(n) time and O(1) space.
Conclusion
LeetCode 1732: Find the Highest Altitude is a classic running-sum problem that demonstrates how cumulative calculations can be performed efficiently without storing unnecessary data.
By maintaining the current altitude and continuously updating the highest altitude encountered, we obtain a clean and optimal solution that runs in linear time with constant space.
While the problem is straightforward, the underlying prefix-sum concept is extremely important and appears repeatedly in coding interviews and competitive programming.




