1732. Find the Highest Altitude LeetCode Solution

In this guide, you will get 1732. Find the Highest Altitude LeetCode Solution with the best time and space complexity. The solution to Find the Highest Altitude problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Find the Highest Altitude solution in C++
  4. Find the Highest Altitude solution in Java
  5. Find the Highest Altitude solution in Python
  6. Additional Resources
1732. Find the Highest Altitude LeetCode Solution image

Problem Statement of 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.

Example 1:

Input: gain = [-5,1,5,0,-7]
Output: 1
Explanation: The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.

Example 2:

Input: gain = [-4,-3,-2,-1,4,3,2]
Output: 0
Explanation: The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.

Constraints:

n == gain.length
1 <= n <= 100
-100 <= gain[i] <= 100

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(1)

1732. Find the Highest Altitude LeetCode Solution in C++

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;
  }
};
/* code provided by PROGIEZ */

1732. Find the Highest Altitude LeetCode Solution in Java

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;
  }
}
// code provided by PROGIEZ

1732. Find the Highest Altitude LeetCode Solution in Python

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
# code by PROGIEZ

Additional Resources

See also  3367. Maximize Sum of Weights after Edge Removals LeetCode Solution

Happy Coding! Keep following PROGIEZ for more updates and solutions.