0% completed
Problem Statement
A bike rider is going on a ride. The road contains n + 1
points at different altitudes. The rider starts from point 0
at an altitude of 0
.
Given an array of integers gain
of length n
, where gain[i]
represents the net gain in altitude between points i
and i + 1
for all (0 <= i < n)
, return the highest altitude
of a point.
Examples
Example 1
- Input: gain =
[-5, 1, 5, 0, -7]
- Expected Output:
1
- Justification: The altitude changes are
[-5, -4, 1, 1, -6]
, where1
is the highest altitude reached.
Example 2
- Input: gain =
[4, -3, 2, -1, -2]
- Expected Output:
4
- Justification: The altitude changes are
[4, 1, 3, 2, 0]
, where4
is the highest altitude reached.
Example 3
- Input: gain =
[2, 2, -3, -1, 2, 1, -5]
- Expected Output:
4
- Justification: The altitude changes are
[2, 4, 1, 0, 2, 3, -2]
, where4
is the highest altitude reached.
Constraints:
n == gain.length
1 <= n <= 100
-100 <= gain[i] <= 100
Solution
To solve this problem, we'll track the cumulative altitude as we traverse the array of altitude changes. We start with an initial altitude of zero. As we iterate through the array, we continually update the current altitude by adding the current change (which could be positive, negative, or zero) to it.
Simultaneously, we keep track of the highest altitude reached so far. This can be done by comparing the current altitude with the maximum altitude recorded after each update. Once we have gone through all the changes, the recorded maximum altitude is the answer. This approach efficiently calculates the highest altitude with a single pass through the array, ensuring optimal time complexity.
Here is the solution with detailed steps.
-
Initialize Variables:
- Start by initializing two variables:
currentAltitude
andmaxAltitude
. - Set both
currentAltitude
andmaxAltitude
to 0, as the starting point is considered at sea level (zero altitude).
- Start by initializing two variables:
-
Iterate Through the Array:
- Loop through each element of the given array of altitude changes.
- For each element in the array, consider it as a change in altitude (gain or loss).
-
Update Current Altitude:
- During each iteration, add the current element (altitude change) to
currentAltitude
.
- During each iteration, add the current element (altitude change) to
-
Check and Update Maximum Altitude:
- After updating
currentAltitude
, compare it withmaxAltitude
. - If
currentAltitude
is greater thanmaxAltitude
, updatemaxAltitude
with the value ofcurrentAltitude
. - This ensures that
maxAltitude
always holds the highest altitude reached so far.
- After updating
-
Continue Until the End of the Array:
- Continue the process of updating
currentAltitude
and checking/updatingmaxAltitude
until you reach the end of the array.
- Continue the process of updating
-
Return the Highest Altitude:
- After completing the iteration through the entire array,
maxAltitude
will hold the highest altitude reached during the journey. - Return
maxAltitude
as the final result.
- After completing the iteration through the entire array,
Algorithm Walkthrough
- Initialize
currentAltitude = 0
andmaxAltitude = 0
. - Loop begins:
i = 0
: Add2
tocurrentAltitude
=>currentAltitude = 2
.maxAltitude
is updated to2
.i = 1
: Add2
tocurrentAltitude
=>currentAltitude = 4
.maxAltitude
is updated to4
.i = 2
: Add-3
tocurrentAltitude
=>currentAltitude = 1
.maxAltitude
remains4
.i = 3
: Add-1
tocurrentAltitude
=>currentAltitude = 0
.maxAltitude
remains4
.i = 4
: Add2
tocurrentAltitude
=>currentAltitude = 2
.maxAltitude
remains4
.i = 5
: Add1
tocurrentAltitude
=>currentAltitude = 3
.maxAltitude
remains4
.i = 6
: Add-5
tocurrentAltitude
=>currentAltitude = -2
.maxAltitude
remains4
.
- Loop ends.
maxAltitude
which is4
is returned as the output.
Code
Here is the code for this algorithm:
Complexity Analysis
Time Complexity
-
Single pass: The algorithm iterates through the
gain
array once, processing each element to update thecurrentAltitude
and check themaxAltitude
. This requires O(N) time, whereN
is the length of thegain
array. -
No nested loops or repeated operations are present, so the time complexity remains linear.
Overall time complexity: O(N).
Space Complexity
-
Constant space: The algorithm only uses a few extra variables (
currentAltitude
andmaxAltitude
), both of which require constant space, O(1). -
No additional data structures (like arrays or lists) are used that scale with the input size.
Overall space complexity: O(1).
Table of Contents
Problem Statement
Examples
Solution
Algorithm Walkthrough
Code
Complexity Analysis
Time Complexity
Space Complexity