Arena / Problems /

1732. Find the Highest Altitude

Easy 0/10 Thrones Claimed
Prev Next

Problem Specification

LeetCode #1732

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
Topics & Algorithms
Hints (2)
Hint 1

Let's note that the altitude of an element is the sum of gains of all the elements behind it

Hint 2

Getting the altitudes can be done by getting the prefix sum array of the given array

⚔️

Vacant Python Throne

No developer has claimed the Python throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

class Solution:
    def largestAltitude(self, gain: List[int]) -> int:
        current_altitude = 0
        max_altitude = 0

        for g in gain:
            current_altitude += g
            if current_altitude > max_altitude:
                max_altitude = current_altitude

        return max_altitude
⚔️

Vacant Java Throne

No developer has claimed the Java throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

class Solution {
    public int largestAltitude(int[] gain) {
        int currentAltitude = 0;
        int maxAltitude = 0;

        for (int g : gain) {
            currentAltitude += g;
            if (currentAltitude > maxAltitude) {
                maxAltitude = currentAltitude;
            }
        }

        return maxAltitude;
    }
}
⚔️

Vacant Javascript Throne

No developer has claimed the Javascript throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

/**
 * @param {number[]} gain
 * @return {number}
 */
var largestAltitude = function(gain) {
    let currentAltitude = 0;
    let maxAltitude = 0;

    for (const g of gain) {
        currentAltitude += g;
        if (currentAltitude > maxAltitude) {
            maxAltitude = currentAltitude;
        }
    }

    return maxAltitude;
};
⚔️

Vacant Cpp Throne

No developer has claimed the Cpp throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

class Solution {
public:
    int largestAltitude(vector<int>& gain) {
        int currentAltitude = 0;
        int maxAltitude = 0;

        for (int g : gain) {
            currentAltitude += g;
            if (currentAltitude > maxAltitude) {
                maxAltitude = currentAltitude;
            }
        }

        return maxAltitude;
    }
};
⚔️

Vacant Csharp Throne

No developer has claimed the Csharp throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

public class Solution {
    public int LargestAltitude(int[] gain) {
        int currentAltitude = 0;
        int maxAltitude = 0;

        foreach (int g in gain) {
            currentAltitude += g;
            if (currentAltitude > maxAltitude) {
                maxAltitude = currentAltitude;
            }
        }

        return maxAltitude;
    }
}
⚔️

Vacant Go Throne

No developer has claimed the Go throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

func largestAltitude(gain []int) int {
    currentAltitude := 0
    maxAltitude := 0

    for _, g := range gain {
        currentAltitude += g
        if currentAltitude > maxAltitude {
            maxAltitude = currentAltitude
        }
    }

    return maxAltitude
}
⚔️

Vacant Ruby Throne

No developer has claimed the Ruby throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

# @param {Integer[]} gain
# @return {Integer}
def largest_altitude(gain)
    current_altitude = 0
    max_altitude = 0

    gain.each do |g|
        current_altitude += g
        max_altitude = current_altitude if current_altitude > max_altitude
    end

    max_altitude
end
⚔️

Vacant Rust Throne

No developer has claimed the Rust throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

impl Solution {
    pub fn largest_altitude(gain: Vec<i32>) -> i32 {
        let mut current_altitude = 0;
        let mut max_altitude = 0;

        for g in gain {
            current_altitude += g;
            if current_altitude > max_altitude {
                max_altitude = current_altitude;
            }
        }

        max_altitude
    }
}
⚔️

Vacant Kotlin Throne

No developer has claimed the Kotlin throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

class Solution {
    fun largestAltitude(gain: IntArray): Int {
        var currentAltitude = 0
        var maxAltitude = 0

        for (g in gain) {
            currentAltitude += g
            if (currentAltitude > maxAltitude) {
                maxAltitude = currentAltitude
            }
        }

        return maxAltitude
    }
}
⚔️

Vacant Swift Throne

No developer has claimed the Swift throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

class Solution {
    func largestAltitude(_ gain: [Int]) -> Int {
        var currentAltitude = 0
        var maxAltitude = 0

        for g in gain {
            currentAltitude += g
            if currentAltitude > maxAltitude {
                maxAltitude = currentAltitude
            }
        }

        return maxAltitude
    }
}
⚔️

Vacant C++ Throne

No developer has claimed the C++ throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

class Solution {
public:
    int largestAltitude(vector<int>& gain) {
        int currentAltitude = 0;
        int maxAltitude = 0;

        for (int g : gain) {
            currentAltitude += g;
            if (currentAltitude > maxAltitude) {
                maxAltitude = currentAltitude;
            }
        }

        return maxAltitude;
    }
};
⚔️

Vacant C# Throne

No developer has claimed the C# throne for LeetCode #1732 yet. Submit an optimal idiomatic solution to become the reigning king and display your portfolio dofollow backlink to visiting engineers worldwide.

Contenders & Community Submissions (1)

Peer solutions & challenges
#1 Prefix Sum by Anonymous Developer
0
Approach Intuition:

The problem requires finding the highest altitude reached during a road trip. The biker starts at altitude 0. The gain array represents the change in altitude between consecutive points.

This implies that the altitude at point i is the sum of all gains up to point i-1.

We can maintain a running sum (prefix sum) to represent the current altitude and continuously update the maximum altitude encountered so far.

public class Solution {
    public int LargestAltitude(int[] gain) {
        int currentAltitude = 0;
        int maxAltitude = 0;

        foreach (int g in gain) {
            currentAltitude += g;
            if (currentAltitude > maxAltitude) {
                maxAltitude = currentAltitude;
            }
        }

        return maxAltitude;
    }
}