넘치게 채우기

[LeetCode] 1732. Find the Highest Altitude 본문

PS/LeetCode

[LeetCode] 1732. Find the Highest Altitude

riveroverflow 2023. 6. 19. 15:09
728x90
반응형

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

 

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

문제 난이도 : Easy

 

문제

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.

 

높이 증감값 배열 gain이 주어진다. 가장 높은 고도를 구하여라.

 

풀이

현재 고도와 가장 높은 고도를 저장하는 변수를 담아서 비교해주면 된다.

 

코드(C++)

class Solution {
public:
    int largestAltitude(vector<int>& gain) {
        int answer = 0;
        int current_height = 0;
        for(int i = 0; i < gain.size(); i++){
            current_height += gain[i];
            answer = max(current_height, answer);
        }
        return answer;
    }
};
 
728x90
반응형