Skip to main content
Back to problems
#1732
Easy Algorithms

Find the highest altitude

Array Prefix Sum
83.9% acceptance
Feb 25, 2026
3235
425
There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes. The biker starts his trip on point 0 with altitude equal 0. You are given an integer array gain of length n where gain[i] is the net gain in altitude between points i and i + 1 for all (0 <= i < n). Return the highest altitude of a point.

Solution

Rust
Time O(n)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn largest_altitude(gain: Vec<i32>) -> i32 {
    let mut alt = 0;
    let mut max_alt = 0;
    for g in gain {
      alt += g;
      max_alt = max_alt.max(alt);
    }
    max_alt
  }
}