Show problems as
Classic problem #1480EasyArrayPrefix Sum

Running Sum of 1d Array

Real-world version: Climb Altitude Log
Tracked in this browser only
Read Running Sum of 1d Array on LeetCode(opens in a new tab)

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

Python
def altitude_log(deltas: list[int]) -> list[int]:
Java
public int[] altitudeLog(int[] deltas)