1732. Find the Highest Altitude

INTRODUCTION

The problem statement says, we are given an array of altitudes of a rider. Now we have to sum them up and in the end check it with max value. If the sum value is larger than the max value then set the max value and return the value. But when adding the values we have to add the altitudes value with the previous value and the starting value is 0.

Examples

HINTS

STEPS

  1. take 2 variable, arr = 0 and max = 0;
  2. Run a for loop and set, arr += gain[i];
  3. Check if (max < arr), set max = arr;
  4. return max.

JavaCode

class Solution {
public int largestAltitude(int[] gain) {
int max = 0;
int arr;
arr= 0;
for(int i = 0; i < gain.length; i++){
arr += gain[i];
if(max < arr){
max = arr;
}
}
return max;
}
}
class Solution {
    public int largestAltitude(int[] gain) {
        int max = 0;
        int arr;
        arr= 0;
        for(int i = 0; i < gain.length; i++){
                arr += gain[i];
            if(max < arr){
                max = arr;
            }
        }
        return max;
    }
}
class Solution { public int largestAltitude(int[] gain) { int max = 0; int arr; arr= 0; for(int i = 0; i < gain.length; i++){ arr += gain[i]; if(max < arr){ max = arr; } } return max; } }

Enter fullscreen mode Exit fullscreen mode

OUTPUT

原文链接:1732. Find the Highest Altitude

© 版权声明
THE END
喜欢就支持一下吧
点赞14 分享
Seeing your adorable smile is the absolute best part of my day.
看见你可爱的笑容绝对是我一天中最美好的事
评论 抢沙发

请登录后发表评论

    暂无评论内容