Find the Highest Altitude — LeetCode 1732 Python Solution
EasyArrayPrefix Sum
- Problem
- #1732
- Pattern
- Prefix Sum
- Reading time
- 2 min
- Source
- leetcode.com
The problem
There is a biker going on a road trip. The road trip consists of n + 1 points at different altitudes.
Example
- Input
- gain = [-5,1,5,0,-7]
- Output
- 1
- Explanation
- The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.
Python solution
Python
class Solution:
def largestAltitude(self, gain: List[int]) -> int:
return max(accumulate(gain, initial=0))Complexity
| Measure | Complexity |
|---|---|
| Time | O(n) |
| Space | O(1) auxiliary |
Pattern: Prefix Sum
Precompute running totals once so any range query becomes a single subtraction. LeetCode 1732. Find the Highest Altitude is filed here on both counts: the reference solution below belongs to the algorithm family this hub collects, and LeetCode tags it Prefix Sum.
The prefix sum guide has the Python template for the pattern and the 157 LeetCode problems that use it.
Related problems
On a study list
This problem is on LeetCode 75.
Frequently asked questions
- How hard is LeetCode 1732. Find the Highest Altitude?
- LeetCode 1732. Find the Highest Altitude is rated Easy on LeetCode.
- What is the time complexity of LeetCode 1732. Find the Highest Altitude?
- The Python solution on this page runs in O(n).
- What is the space complexity of LeetCode 1732. Find the Highest Altitude?
- The Python solution on this page uses O(1) auxiliary space.
- What topics does LeetCode 1732. Find the Highest Altitude cover?
- LeetCode 1732. Find the Highest Altitude is tagged Array and Prefix Sum on LeetCode.