找到最高海拔【LC1732】
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; } } -
复杂度
- 时间复杂度:O(n)O(n)O(n),n为数组长度
- 空间复杂度:O(1)O(1)O(1)
本文解析了LeetCode上的最高海拔问题,通过遍历数组并使用前缀和的方法求得每个点的海拔高度,最终找到最高点的海拔。介绍了算法的具体实现及时间、空间复杂度。

被折叠的 条评论
为什么被折叠?



