Skip to content

1732. Find the Highest Altitude 👍

  • Time: $O(n)$
  • Space: $O(1)$
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
class Solution {
 public:
  int largestAltitude(vector<int>& gain) {
    int ans = 0;
    int currAltitude = 0;
    for (const int g : gain) {
      currAltitude += g;
      ans = max(ans, currAltitude);
    }
    return ans;
  }
};
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
class Solution {
  public int largestAltitude(int[] gain) {
    int ans = 0;
    int currAltitude = 0;
    for (final int g : gain) {
      currAltitude += g;
      ans = Math.max(ans, currAltitude);
    }
    return ans;
  }
}
1
2
3
4
5
6
7
8
class Solution:
  def largestAltitude(self, gain: list[int]) -> int:
    ans = 0
    currAltitude = 0
    for g in gain:
      currAltitude += g
      ans = max(ans, currAltitude)
    return ans