LeetCode 1480 Running Sum of 1d Array - Example and Complexity Analysis

Walkthrough and analysis

Trace nums = [3, 1, 2, 10, 1].

Start: result = [3, _, _, _, _] (first element unchanged).

i = 1: result[1] = result[0] + nums[1] = 3 + 1 = 4. Result: [3, 4, _, _, _].

i = 2: result[2] = result[1] + nums[2] = 4 + 2 = 6. Result: [3, 4, 6, _, _].

i = 3: result[3] = result[2] + nums[3] = 6 + 10 = 16. Result: [3, 4, 6, 16, _].

i = 4: result[4] = result[3] + nums[4] = 16 + 1 = 17. Result: [3, 4, 6, 16, 17].

One pass through nn elements. O(n)O(n) time.

Modifying in-place uses O(1)O(1) extra space. Creating a new array uses O(n)O(n) space.