0
点赞
收藏
分享

微信扫一扫

LeetCode知识点总结 - 1732

编程练习生J 2022-01-09 阅读 60

LeetCode 1732. Find the Highest Altitude

考点难度
Prefix SumEasy
题目

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;
}
举报

相关推荐

0 条评论