There is a biker going on a road trip. The road trip consists of
n + 1points at different altitudes. The biker starts his trip on point0with altitude equal0.You are given an integer array
gainof lengthnwheregain[i]is the net gain in altitude between pointsiandi + 1for all (0 <= i < n). Return the highest altitude of a point.
有一个自行车手打算进行一场公路骑行,这条路线总共由
n + 1个不同海拔的点组成。自行车手从海拔为0的点0开始骑行。给你一个长度为
n的整数数组gain,其中gain[i]是点i和点i + 1的 净海拔高度差(0 <= i < n)。请你返回 最高点的海拔 。
美好的一天从leetcode开始
思路:遍历数组,使用前缀和求每一个点的海拔高度,取最大值返回既可
实现
class Solution {public int largestAltitude(int[] gain) {int res = 0;int height = 0;for (int i = 0; i < gain.length; i++){height += gain[i];res = Math.max(res,height);}return res;}
}
复杂度