1732. Find the Highest Altitude

Leetcode link

题目简介

/**
 * @param {number[]} gain
 * @return {number}
 */

题目给我们一个数字数组 gain 表示高度的变化(高度从 0 开始)

要求我们计算出高度最高的点有多高

返回最高的高度

解题思路

我们只需要用变量 altitude 来记录当前高度、res 来记录最高的高度

然后遍历 gain 数组即可,遍历时每次用 altitude+gain[i],然后更新 res 即可

最后返回 res

Javascript

/**
 * @param {number[]} gain
 * @return {number}
 */
var largestAltitude = function(gain) {
    let res = 0
    let altitude = 0
    for(const g of gain) {
        altitude += g
        res = Math.max(res, altitude)
    }
    return res
};

results matching ""

    No results matching ""