3. Cumulative Steps

A pedometer app logs how many steps a user took during each hour of the day. The user wants a running tally instead: for every hour, the total number of steps taken from the first hour up to and including that hour.

Write a method runningSum that takes the array nums of hourly step counts and returns an array of the same length whose element i is the sum of nums[0] through nums[i].

Example 1

Input:
nums = [1,2,3,4]
Output:
[1,3,6,10]
Explanation:

The totals after each hour are 1, 1+2=3, 1+2+3=6 and 1+2+3+4=10.

Example 2

Input:
nums = [500,0,250]
Output:
[500,500,750]
Explanation:

An hour with no steps leaves the running total unchanged: 500, 500, 750.

Constraints

1 ≤ nums.length ≤ 1000

0 ≤ nums[i] ≤ 105

How this problem is judged

Answers
Your answer must match exactly. Numbers compare by value, so 2 and 2.0 are equal.

Expected complexity

Time
O(n)
Space
O(n)

What the author was aiming for. Your own solution is not measured against it.

Asked in an interview

Were you asked this in an interview? Say where, anonymously.

Code
Loading the editor…