Skip to content

Commit f1bf57d

Browse files
committedFeb 21, 2025·
Add solution for max altitude
1 parent 83db51a commit f1bf57d

File tree

3 files changed

+65
-0
lines changed

3 files changed

+65
-0
lines changed
 

‎prefix-sum/highest-altitude/README.md

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# 1732. Find the Highest Altitude
2+
3+
## Description
4+
See https://leetcode.com/problems/find-the-highest-altitude/description/
5+
6+
## Problem
7+
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`.
8+
9+
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.
10+
11+
## Example 1
12+
13+
```
14+
Input: gain = [-5,1,5,0,-7]
15+
Output: 1
16+
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
17+
```
18+
19+
## Example 2
20+
21+
```
22+
Input: gain = [-4,-3,-2,-1,4,3,2]
23+
Output: 0
24+
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
25+
```
26+
27+
## Constraints
28+
29+
```
30+
n == gain.length
31+
1 <= n <= 100
32+
-100 <= gain[i] <= 100
33+
```
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
def max_altitude(gain: list[int]) -> int:
2+
max_alt, curr = 0, 0
3+
for i in gain:
4+
curr += i
5+
max_alt = max(max_alt, curr)
6+
return max_alt
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from altitude import max_altitude
2+
3+
4+
def test_example_1():
5+
gain = [-5, 1, 5, 0, -7]
6+
assert max_altitude(gain) == 1
7+
8+
9+
def test_example_2():
10+
gain = [-4, -3, -2, -1, 4, 3, 2]
11+
assert max_altitude(gain) == 0
12+
13+
14+
def test_example_3():
15+
gain = [1, 2, 3, 4, 5]
16+
assert max_altitude(gain) == 15
17+
18+
19+
def test_example_4():
20+
gain = [0, 0, 0, 0, 0]
21+
assert max_altitude(gain) == 0
22+
23+
24+
def test_example_5():
25+
gain = [-1, -2, -3, -4, -5]
26+
assert max_altitude(gain) == 0

0 commit comments

Comments
 (0)
Please sign in to comment.