LeetCode 1732. Find the Highest Altitude
考点 | 难度 |
---|---|
Prefix Sum | Easy |
题目
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 integer array gain
of length n
where gain[i]
is the net gain in altitude between points i
and i + 1
for all (0 <= i < n). Return the highest altitude of a point.
思路
保留一个最大的,再用后面的值(sum加array里后面的值)和最大值比较。
答案
public int largestAltitude(int[] gain) {
int max_alt=0;
int curr_alt=0;
for(int i=0;i<gain.length;i++){
curr_alt+=gain[i];
max_alt=Math.max(curr_alt, max_alt);
}
return max_alt;
}