找到最高海拔

Bernie


Bernie
1732. 找到最高海拔
思路:
遍历 gain 数组,计算当前海拔,如果当前海拔大于最大海拔,则更新最大海拔,最后返回最大海拔即可。
typescript 解法
function largestAltitude(gain: number[]): number {
let max = 0;
let current = 0;
for (let i = 0; i < gain.length; i++) {
current += gain[i];
if (current > max) {
max = current;
}
}
return max;
}
go 解法
func largestAltitude(gain []int) int {
max := 0
current := 0
for _, v := range gain {
current = current + v
if current > max {
max = current
}
}
return max
}