classSolution { public: intlargestAltitude(vector<int>& gain){ int ans = 0, sum = 0; for (int i = 0; i < gain.size(); i++) { sum += gain[i]; ans = max(ans, sum); } return ans; } };
Python Version
1 2 3 4 5 6 7
classSolution: deflargestAltitude(self, gain: List[int]) -> int: ans = sum = 0 for i in gain : sum += i ans = max(sum, ans) return ans