Skip to content

Find the Highest Altitude

EasyArrays & Hashing
Decode it first. Clues, constraints, pattern and plan before any code.

A cyclist starts a ride at altitude 0. You are given gain, where gain[i] is the net change in altitude between point i and point i + 1. Return the highest altitude reached at any point, including the start.

gain = [-5, 1, 5, 0, -7]          →  1   (altitudes 0, -5, -4, 1, 1, -6)
gain = [-4, -3, -2, -1, 4, 3, 2]  →  0   (never climbs above the start)

Constraints

  • n == gain.length
  • 1 <= n <= 100
  • -100 <= gain[i] <= 100