#1732

Find the Highest Altitude

Easy Running sum

  1. Read
  2. Understand
  3. Hints
  4. Approach
  5. Solution
  6. Solved

?The problem

A biker starts at altitude 0. gain[i] is the change in altitude between point i and point i + 1. Return the highest altitude reached.

This is a short summary in our own words. Read the full statement and examples on LeetCode: open problem ↗

◎Understanding

Really asking: the altitude after each step is a running total of the gains; what's the highest point?

Why this works: keep the running total and its maximum; the start (0) counts too.

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

altitudes: 0, -5, -4, 1, 1, -6highest = 1

✦Hints open one at a time

↗Approach

</>Solution (Java)

!Common mistake

Initialising the maximum to the first gain — if all gains are negative, the answer is 0.