Back to Problem
Official 1386 views

Editorial: Running Sum of an Array

by admin ·

34

1 Intuition

Each prefix sum is the previous prefix sum plus the current element.

2 Approach

Walk the array once, keeping a running total, and append the total after adding each element.

3 Complexity Analysis

Time O(n), Space O(n) for the output.

4 Solution Code

class Solution:
    def runningSum(self, nums: list[int]) -> list[int]:
        out, total = [], 0
        for x in nums:
            total += x
            out.append(total)
        return out

5 Code Walkthrough

The reference solution below implements the approach step by step.