[LeetCode] Find the Highest Altitude

1732. Find the Highest Altitude

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.

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.

1
2
3
4
5
6
7
8
class Solution {
public:
int largestAltitude(vector<int>& gain) {
int res = 0, now = 0;
for(auto g : gain) res = max(res, now += g);
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/06/19/PS/LeetCode/find-the-highest-altitude/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.