Problem 259569 · easy · Phase 02 Linear Data Structures

Running Totals

arrays · prefix sums · accumulation

Given a list of integers nums, return a new list out of the same length where out[i] is the sum of nums[0] through nums[i] inclusive.

Examples

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

Input:  nums = [5, -5, 5]
Output: [5, 0, 5]

Constraints

  • 0 <= len(nums) <= 10**5
  • -10**4 <= nums[i] <= 10**4
  • Return a new list.
  • Target: O(n) time; recomputing sum(nums[:i+1]) for each i is too slow.

Goals

  • Carry an accumulator through a single pass
  • Build a prefix-sum list of the same length as the input
  • Handle negatives and the empty list
Starting Python…