Beats 94.41% || Easy Solution 🤓✅
\n\n\n# Intuition\nThe problem is asking for the largest altitude reached given a series of altitude gains and losses during a bike ride. My first thought is to
![image.png](https://assets.leetcode.com/users/images/8a07722c-9a63-4059-8c67-84459aabf280_1728128992.9938369.png)\n\n\n# Intuition\nThe problem is asking for the largest altitude reached given a series of altitude gains and losses during a bike ride. My first thought is to maintain a running total of the current altitude as we go through each gain/loss, and at each step, check if the current altitude is the highest we have encountered so far. This would allow us to track the largest altitude efficiently in a single pass.\n\n# Approach\n1. **Initialize two variables**: One to keep track of the current altitude (`current`) and another to store the maximum altitude encountered so far (`max_gain`).\n2. **Iterate through the `gain` array**: For each element, update the `current` altitude by adding the current gain/loss value to the previous `current` value.\n3. **Update `max_gain`**: After updating the current altitude, check if it\u2019s the highest altitude encountered. If it is, update `max_gain`.\n4. **Return `max_gain`** after finishing the loop, as it represents the largest altitude reached during the ride.\n\n# Complexity\n- Time complexity:\n  The time complexity is $$O(n)$$ where $$n$$ is the number of elements in the `gain` list. We only iterate through the list once.\n\n- Space complexity:\n  The space complexity is $$O(1)$$ since we only use two extra variables (`current` and `max_gain`) regardless of the input size.\n\n# Code\n```python\nclass Solution:\n    def largestAltitude(self, gain: List[int]) -> int:\n        current = 0\n        max_gain = current\n        for alt in gain:\n            current += alt\n            max_gain = max(max_gain, current)\n        return max_gain
['Prefix Sum', 'Python3']