FastPrepMaximum Later Checkpoint Altitude Gain

Maximum Later Checkpoint Altitude Gain

Yellow.ai logoYellow.ai● EasyFULLTIMEPHONE SCREEN

Problem statement

A trek records checkpoint altitudes in their visit order in trek. Choose one checkpoint and a strictly later checkpoint. Return the largest positive later altitude minus earlier altitude.

The checkpoints need not be adjacent. Return 0 when no positive gain is possible, including when there are fewer than two checkpoints. Return the exact result as a 64-bit integer.

Function

maximumAltitudeGain(trek: int[]) → long

Examples

Example 1

trek = [1000,1200,800,1175,700,1500]return = 800

Checkpoint 4 has altitude 700 and the later checkpoint 5 has altitude 1500. Their gain is 800, the largest among all 15 ordered checkpoint pairs. The source printed 700; that conflicting line is not the judged result.

Example 2

trek = [9,7,4,1]return = 0

Every later checkpoint is lower, so there is no positive gain and the result is 0.

Constraints

  • 0 <= trek.length <= 100000.
  • -2147483648 <= trek[i] <= 2147483647.

More Yellow.ai problems

See Yellow.ai hiring insights
Saved
public long maximumAltitudeGain(int[] trek) {
    // Write your code here.
}
Checking account…