June 15th 2023

Find the Highest Altitude — Typescript

Problem Description permalink

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.

Solution permalink

function expect(description: string, test: boolean) {
if (test) {
console.log(`${description}`)
} else {
console.log(`${description}`)
}
}

function largestAltitude(gain: number[]): number {
var highestPoint = 0;
var currentPoint = 0;

for (var currentGain of gain) {
currentPoint += currentGain
if (currentPoint > highestPoint) {
highestPoint = currentPoint
}
}

return highestPoint
};

expect("largestAltitude([-5,1,5,0,-7]) == 1", largestAltitude([-5, 1, 5, 0, -7]) == 1)

Discussion permalink