Skip to content

Commit 3c83a0c

Browse files
authored
Merge pull request #7 from 149ps/LC-problems
O(n) time and O(1) space
2 parents a1289af + a70b5f5 commit 3c83a0c

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
"""
2+
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.
3+
4+
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.
5+
6+
7+
8+
Example 1:
9+
10+
Input: gain = [-5,1,5,0,-7]
11+
Output: 1
12+
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
13+
Example 2:
14+
15+
Input: gain = [-4,-3,-2,-1,4,3,2]
16+
Output: 0
17+
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.
18+
19+
20+
Constraints:
21+
22+
n == gain.length
23+
1 <= n <= 100
24+
-100 <= gain[i] <= 100
25+
"""
26+
class Solution:
27+
def largestAltitude(self, gain: List[int]) -> int:
28+
result,cur = -sys.maxsize-1,0
29+
for num in gain:
30+
cur += num
31+
result = max(result, cur)
32+
return max(result,0)

0 commit comments

Comments
 (0)