Leetcode/LeetCode75

[PrefixSum][Easy] 1732. Find the Highest Altitude

자전거통학 2023. 12. 7. 08:05

https://leetcode.com/problems/find-the-highest-altitude/solutions

 

Find the Highest Altitude - LeetCode

Can you solve this real interview question? 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 integ

leetcode.com

 

Q. 입력된 배열은 해당 위치에서 이전 위치까지의 고도차를 의미한다. 이때 최대 고도를 가진 지점을 찾아라.

Solution. 0 에서 시작하는 고도에서 값을 누적하여 절대치를 찾고 그 위치중 가장 높은 지점을 찾는 문제. 

 값 누적해서 그 중 최대값을 찾으면 된다. LeetCode에서 가장 쉬운 문제 중 하나. 

 

int largestAltitude(vector<int>& gain)
{
    if (gain.size() < 1)
        return 0;
    int elapsed = gain[0];
    int ret = max(0, elapsed);
    for (int k = 1; k < gain.size(); ++k)
    {
        elapsed += gain[k];
        ret = max(ret, elapsed);
    }
    return ret;
}