DEV Community

Cover image for 1732. Find the Highest Altitude
Tahzib Mahmud Rifat
Tahzib Mahmud Rifat

Posted on

1732. Find the Highest Altitude

INTRODUCTION

Image description

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

Image description

HINTS

Image description

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;
    }
}
Enter fullscreen mode Exit fullscreen mode

OUTPUT

Image description

Top comments (0)

Great read:

Is it Time to go Back to the Monolith?

History repeats itself. Everything old is new again and I’ve been around long enough to see ideas discarded, rediscovered and return triumphantly to overtake the fad. In recent years SQL has made a tremendous comeback from the dead. We love relational databases all over again. I think the Monolith will have its space odyssey moment again. Microservices and serverless are trends pushed by the cloud vendors, designed to sell us more cloud computing resources.

Microservices make very little sense financially for most use cases. Yes, they can ramp down. But when they scale up, they pay the costs in dividends. The increased observability costs alone line the pockets of the “big cloud” vendors.