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.
LeetCode Problem - 1732
class Solution {
// Method to find the largest altitude from a series of altitude gains
public int largestAltitude(int[] gain) {
// Initialize variables to keep track of the current total gain and the highest altitude
int totalGain = 0, answer = 0;
// Iterate through each altitude gain
for(int i = 0; i < gain.length; i++) {
// Update the current total gain by adding the current gain
totalGain += gain[i];
// If the current total gain is greater than the highest recorded altitude, update the highest altitude
if(totalGain > answer) answer = totalGain;
}
// Return the highest altitude achieved
return answer;
}
}