-4 -3 -2 -1 4 3 2 ^ -4 -7 -9 -10 -6 -3 -1
(gain []int)
| 8 | // |
| 9 | // -4 -7 -9 -10 -6 -3 -1 |
| 10 | func 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 | } |
no outgoing calls