Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit a70b5f5

Browse files
committed
O(n) time and O(1) space
1 parent dd78621 commit a70b5f5

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)

AltStyle によって変換されたページ (->オリジナル) /