Imagine you're tracking a mountain biker's journey through a scenic route with varying elevations. The biker starts at sea level (altitude 0) and travels through n + 1 checkpoints, each at different heights.
You're given an array gain where gain[i] represents the net change in altitude between checkpoint i and checkpoint i + 1. A positive value means going uphill, while a negative value means going downhill.
Your mission: Find the highest altitude the biker reaches during their entire journey.
Example: If gain = [-5, 1, 5, 0, -7], the biker's altitude at each point would be: [0, -5, -4, 1, 1, -6], so the highest altitude reached is 1.
Input & Output
Visualization
Time & Space Complexity
We iterate through the gain array exactly once, performing constant time operations
Only using two variables to track current and maximum altitude
Constraints
- n == gain.length
- 1 ≤ n ≤ 100
- -100 ≤ gain[i] ≤ 100