Running Sum of 1d Array
Real-world version: Climb Altitude LogThe statement here is our own: the same inputs, outputs and technique.
A trekking watch splits a walk into legs and stores one signed altitude change per leg in deltas, measured in metres. A positive entry is a climb, a negative entry is a descent, and a zero entry is flat ground.
The watch starts the walk at altitude 0. Build the log the walker actually wants: an array of the same length whose entry i is the altitude reached once leg i has been finished, which is the sum of deltas[0] through deltas[i].
Altitudes may sit below the starting point, so entries in the answer can be negative.
Examples
Example 1
- Input
- deltas = [12, -5, 40, 0]
- Output
- [12, 7, 47, 47]
After the first leg the walker is 12 metres up, the descent brings that to 7, the long climb takes it to 47, and the flat leg leaves it there.
Example 2
- Input
- deltas = [-3, -3, -3]
- Output
- [-3, -6, -9]
Every leg drops three metres, so the log runs steadily below the starting altitude.
Example 3
- Input
- deltas = [7]
- Output
- [7]
A single leg gives a single altitude, which is that leg's own change.
Constraints
- 1 <= deltas.length <= 1000
- -10^6 <= deltas[i] <= 10^6
The signature
The editor starts with this. It matches the parent problem's shape, so a solution that works here works on a standard judge unchanged.
def altitude_log(deltas: list[int]) -> list[int]:public int[] altitudeLog(int[] deltas)Checking your plan…
How this problem was checked
- Passed: Reference solution run against 10 test cases: 10 passed
- Passed: 3 worked examples confirmed by running the code
- Passed: 0 eight-word sequences shared with the parent problem's text
- Passed: Agrees with an independent brute-force solution on random inputs
- Passed: Test set shown to catch deliberately broken solutions
- Passed: Reviewed by a separate quality pass
- Passed: Function arity matches the parent problem's