LeetCode 1732: Find the Highest Altitude – Java Prefix Sum Solution Explained

Master Running Sum and Prefix Sum Techniques with Step-by-Step Explanation, Dry Run, Alternative Approaches, Complexity Analysis, and Interview Insights

Krishna Shrivastava
8 views
LinkedInGithubX
0
0
LeetCode 1732: Find the Highest Altitude – Java Prefix Sum Solution Explained
Listen to articleAudio version
Ad

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:

0

You are given an array:

gain[i]

where:

gain[i]

represents the change in altitude between point i and point i + 1.

Return the highest altitude reached during the entire trip.

Example 1

Input

gain = [-5,1,5,0,-7]

Altitude Calculation

Start = 0

0 + (-5) = -5
-5 + 1 = -4
-4 + 5 = 1
1 + 0 = 1
1 + (-7) = -6

Altitudes:

[0, -5, -4, 1, 1, -6]

Output

1

Example 2

Input

gain = [-4,-3,-2,-1,4,3,2]

Altitudes

[0,-4,-7,-9,-10,-6,-3,-1]

Output

0

The biker never goes above the starting altitude.

Key Observation

We are not required to store all altitudes.

Instead:

  1. Keep track of the current altitude.
  2. Update it using each gain value.
  3. 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:

gain = [-5,1,5,0,-7]

Initialize:

currentAltitude = 0
maxAltitude = 0

Process each value:

GainCurrent AltitudeMaximum Altitude
-5-50
1-40
511
011
-7-61

Final answer:

1

Java Solution

class Solution {

public int largestAltitude(int[] gain) {

int max = 0;
int altitude = 0;

for (int value : gain) {

altitude += value;

max = Math.max(max, altitude);
}

return max;
}
}

Dry Run

Input

gain = [-5,1,5,0,-7]

Initial State

altitude = 0
max = 0

Step 1

altitude += -5

Result:

altitude = -5
max = 0

Step 2

altitude += 1

Result:

altitude = -4
max = 0

Step 3

altitude += 5

Result:

altitude = 1
max = 1

Step 4

altitude += 0

Result:

altitude = 1
max = 1

Step 5

altitude += -7

Result:

altitude = -6
max = 1

Final Answer

1

Alternative Approach: Build Complete Altitude Array

Another way is to explicitly construct all altitudes.

class Solution {

public int largestAltitude(int[] gain) {

int[] altitude = new int[gain.length + 1];

int max = 0;

for (int i = 1; i < altitude.length; i++) {

altitude[i] = altitude[i - 1] + gain[i - 1];

max = Math.max(max, altitude[i]);
}

return max;
}
}

Why This Is Less Optimal

  1. Requires extra memory.
  2. Stores values we don't actually need.
  3. Same time complexity but worse space complexity.

Optimized Approach (Recommended)

Instead of storing every altitude:

  1. Maintain a running altitude.
  2. Update the maximum altitude immediately.

Benefits:

  1. Single pass.
  2. Constant extra memory.
  3. Cleaner implementation.

Complexity Analysis

Time Complexity

O(n)

We traverse the array exactly once.

Space Complexity

O(1)

Only a few variables are used.

Common Mistakes

Mistake 1: Forgetting the Starting Altitude

The biker starts at:

0

This altitude must be considered when finding the maximum.

Example:

gain = [-4,-3,-2]

Highest altitude:

0

not:

-4

Mistake 2: Using Only Individual Gain Values

Some beginners try:

max = Math.max(max, gain[i]);

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:

  1. Prefix Sum fundamentals
  2. Running Sum techniques
  3. Cumulative calculations
  4. Maximum tracking patterns

These concepts frequently appear in:

  1. Array problems
  2. Dynamic Programming
  3. Sliding Window problems
  4. Financial data calculations
  5. Path and graph simulations

Mastering this pattern helps solve many more advanced interview questions.

Key Takeaways

  1. Start altitude is always 0.
  2. Each gain modifies the current altitude.
  3. Use a running sum to calculate altitude.
  4. Track the maximum altitude during traversal.
  5. No extra array is required.
  6. 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.

Ai Assistant Kas