MCPcopy Create free account
hub / github.com/austingebauer/go-leetcode / largestAltitude

Function largestAltitude

find_the_highest_altitude_1732/solution.go:10–26  ·  view source on GitHub ↗

-4 -3 -2 -1 4 3 2 ^ -4 -7 -9 -10 -6 -3 -1

(gain []int)

Source from the content-addressed store, hash-verified

8//
9// -4 -7 -9 -10 -6 -3 -1
10func largestAltitude(gain []int) int {
11 prefixSums := make([]int, len(gain))
12 for i, g := range gain {
13 toAdd := 0
14 if i > 0 {
15 toAdd = prefixSums[i-1]
16 }
17 prefixSums[i] = toAdd + g
18 }
19
20 // catch is that zero starting point could be the highest altitude
21 highest := 0
22 for _, s := range prefixSums {
23 highest = int(math.Max(float64(highest), float64(s)))
24 }
25 return highest
26}

Callers 1

Test_largestAltitudeFunction · 0.85

Calls

no outgoing calls

Tested by 1

Test_largestAltitudeFunction · 0.68