You are given an integer array called gain
of length n
where gain[i]
represents the net gain in altitude between points i
and i + 1
on a bike trip. The initial altitude before the trip is 0
. Your task is to return the highest altitude reached during the trip.
0
.gain
represents the change in altitude from the previous point.
The problem is about tracking altitude changes as we move through the gain
array. At each step, we add the current gain to the previous altitude to get the new altitude. We want to keep track of the highest altitude we reach as we go.
A brute-force approach would be to generate all possible altitude values step by step and then find the maximum. But since the altitude at each step only depends on the sum of gains up to that point, we can simply keep a running sum and update the maximum whenever the new altitude is higher.
This is similar to keeping track of your cumulative score in a game and noting the highest score you reach at any point.
We solve this problem efficiently by iterating through the gain
array once, keeping track of the current altitude and the maximum altitude seen so far.
current_altitude = 0
and max_altitude = 0
.g
in gain
:
g
to current_altitude
.current_altitude
is greater than max_altitude
, update max_altitude
.max_altitude
holds the highest altitude reached.This approach is efficient because we only need a single pass through the array and constant extra space.
Let's walk through an example with gain = [-5, 1, 5, 0, -7]
.
0
(initial).-5
): altitude = 0 + (-5) = -5
1
): altitude = -5 + 1 = -4
5
): altitude = -4 + 5 = 1
0
): altitude = 1 + 0 = 1
-7
): altitude = 1 + (-7) = -6
The altitudes at each point are: 0, -5, -4, 1, 1, -6
. The highest is 1
.
gain
and keep two variables. This gives us O(n) time complexity and O(1) space complexity, where n
is the length of gain
.
The solution involves a simple running sum to track the current altitude and a variable to record the maximum seen so far. By updating these as we iterate through the gain
array, we efficiently determine the highest altitude reached. This approach is both time and space optimal, and demonstrates the power of cumulative tracking in sequential problems.
class Solution:
def largestAltitude(self, gain):
max_altitude = 0
current_altitude = 0
for g in gain:
current_altitude += g
if current_altitude > max_altitude:
max_altitude = current_altitude
return max_altitude
class Solution {
public:
int largestAltitude(vector<int>& gain) {
int maxAltitude = 0;
int current = 0;
for (int g : gain) {
current += g;
if (current > maxAltitude)
maxAltitude = current;
}
return maxAltitude;
}
};
class Solution {
public int largestAltitude(int[] gain) {
int maxAltitude = 0;
int current = 0;
for (int g : gain) {
current += g;
if (current > maxAltitude)
maxAltitude = current;
}
return maxAltitude;
}
}
var largestAltitude = function(gain) {
let maxAltitude = 0;
let current = 0;
for (let g of gain) {
current += g;
if (current > maxAltitude)
maxAltitude = current;
}
return maxAltitude;
};