PS/LeetCode

LeetCode / Array / 1732번 / Find the Highest Altitude / JS

KimMinJun 2023. 6. 21. 19:03

< 문제 바로가기 >

 

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

 

< 문제 간단설명 >

고도들이 배열로 주어지는데, 고도들을 따라 계속해서 이동했을 때(누적 합), 가장 고도가 높은 곳의 고도를 반환하면 된다.

 

/**
 * @param {number[]} gain
 * @return {number}
 */
var largestAltitude = function(gain) {
    let altitude = 0;
    let altitudeList = [0];
    
    for(let i=0; i<gain.length; i+=1) {
      altitude += gain[i];
      altitudeList.push(altitude);
    }

    return Math.max(...altitudeList);
};