Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions problems/1732-find-the-highest-altitude/analysis.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# 1732. Find the Highest Altitude

[LeetCode Link](https://leetcode.com/problems/find-the-highest-altitude/)

Difficulty: Easy
Topics: Array, Prefix Sum
Acceptance Rate: 84.1%

## Hints

### Hint 1

The array you are given is not the altitudes themselves — it is the *change* in altitude between consecutive points. Think about what data structure pattern lets you reconstruct cumulative values from a list of deltas. The topic tag "Prefix Sum" is a strong nudge.

### Hint 2

You start at altitude `0`. Each point's altitude is the running total of all the gains seen so far. So instead of building the whole altitude array, ask yourself: do you actually need to store every altitude, or can you track just two things as you sweep left to right?

### Hint 3

Maintain a single running sum that you update with each `gain[i]`, and a separate variable holding the maximum altitude seen. Initialize the maximum to `0` (the starting point itself is a valid altitude). After adding each gain, compare the running sum against the maximum. One pass, no extra array needed.

## Approach

The altitudes form a prefix sum of the `gain` array, anchored at a starting altitude of `0`.

- The biker begins at point `0` with altitude `0`.
- The altitude at point `i + 1` equals `gain[0] + gain[1] + ... + gain[i]`.

So the sequence of altitudes is exactly the running (prefix) sum of `gain`, with `0` prepended for the start.

We want the highest altitude among all `n + 1` points. The naive approach builds the full altitude array and scans it, but we can fold both steps into one pass:

1. Keep a `current` running sum, initialized to `0` (the starting altitude).
2. Keep a `highest` value, also initialized to `0`, because the starting point is a candidate answer.
3. For each `g` in `gain`, add `g` to `current`, then update `highest = max(highest, current)`.
4. Return `highest`.

**Walkthrough with `gain = [-5, 1, 5, 0, -7]`:**

| step | g | current | highest |
|------|----|---------|---------|
| start| — | 0 | 0 |
| 1 | -5 | -5 | 0 |
| 2 | 1 | -4 | 0 |
| 3 | 5 | 1 | 1 |
| 4 | 0 | 1 | 1 |
| 5 | -7 | -6 | 1 |

The reconstructed altitudes are `[0, -5, -4, 1, 1, -6]`, and the highest is `1`. ✅

This is the prefix-sum pattern in its purest form: you never need to materialize the full prefix array when you only care about an aggregate (here, the maximum) over it.

## Complexity Analysis

Time Complexity: O(n) — a single pass over the `gain` array.
Space Complexity: O(1) — only two scalar variables are kept, regardless of input size.

## Edge Cases

- **Starting point is the highest:** When every gain is negative (e.g. `[-4, -3, -2, -1, 4, 3, 2]` from Example 2), the altitude never rises above the starting `0`. Initializing `highest` to `0` ensures we return `0` rather than a negative running sum.
- **Single-element input (`n == 1`):** The constraints guarantee at least one gain, so the array is never empty, but a length-1 array still has two points (start and one move). The single-pass loop handles this naturally.
- **All positive gains:** The altitude is strictly increasing, so the answer is the final running sum — the loop captures it on the last iteration.
- **Mixed gains with a peak in the middle:** The maximum may occur at an interior point and then drop (as in Example 1). Tracking the running maximum, not just the final sum, is essential.
48 changes: 48 additions & 0 deletions problems/1732-find-the-highest-altitude/problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
number: "1732"
frontend_id: "1732"
title: "Find the Highest Altitude"
slug: "find-the-highest-altitude"
difficulty: "Easy"
topics:
- "Array"
- "Prefix Sum"
acceptance_rate: 8414.7
is_premium: false
created_at: "2026-06-19T05:32:20.556971+00:00"
fetched_at: "2026-06-19T05:32:20.556971+00:00"
link: "https://leetcode.com/problems/find-the-highest-altitude/"
date: "2026-06-19"
---

# 1732. Find the Highest Altitude

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._



**Example 1:**


**Input:** gain = [-5,1,5,0,-7]
**Output:** 1
**Explanation:** The altitudes are [0,-5,-4,1,1,-6]. The highest is 1.


**Example 2:**


**Input:** gain = [-4,-3,-2,-1,4,3,2]
**Output:** 0
**Explanation:** The altitudes are [0,-4,-7,-9,-10,-6,-3,-1]. The highest is 0.




**Constraints:**

* `n == gain.length`
* `1 <= n <= 100`
* `-100 <= gain[i] <= 100`
4 changes: 2 additions & 2 deletions problems/1732-find-the-highest-altitude/solution.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package solution
package main

// intuition: you can do this
// https://leetcode.com/problems/find-the-highest-altitude/?envType=study-plan-v2&envId=leetcode-75
func largestAltitude(gain []int) int {
func largestAltitudeStudyPlan(gain []int) int {
max := 0
gained := make([]int, len(gain)+1)
gained[0] = 0
Expand Down
20 changes: 20 additions & 0 deletions problems/1732-find-the-highest-altitude/solution_daily_20260619.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package main

// Find the Highest Altitude (LeetCode 1732)
//
// Approach: prefix sum in a single pass.
// The altitudes are the running (prefix) sum of `gain`, starting from 0.
// We keep one running total and the maximum seen so far, initializing the
// maximum to 0 because the starting point's altitude (0) is a valid answer.
// Time: O(n), Space: O(1).
func largestAltitude(gain []int) int {
highest := 0
current := 0
for _, g := range gain {
current += g
if current > highest {
highest = current
}
}
return highest
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package main

import "testing"

func TestSolution(t *testing.T) {
tests := []struct {
name string
gain []int
expected int
}{
{"example 1: peak at an interior point", []int{-5, 1, 5, 0, -7}, 1},
{"example 2: never rises above start", []int{-4, -3, -2, -1, 4, 3, 2}, 0},
{"edge case: single negative gain", []int{-100}, 0},
{"edge case: single positive gain", []int{100}, 100},
{"edge case: all positive, increasing altitude", []int{1, 2, 3, 4}, 10},
{"edge case: rises then falls below start", []int{5, -10, 5}, 5},
{"edge case: returns to start altitude", []int{3, -3}, 3},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := largestAltitude(tt.gain)
if result != tt.expected {
t.Errorf("largestAltitude(%v) = %d, want %d", tt.gain, result, tt.expected)
}
})
}
}