easy +8 pts

Running Total Window

Compute a running total that resets after hitting zero using a sliding window.

Write a function `running_total_window(nums)` that takes a list of integers `nums` and returns a new list where each element is the cumulative sum of all previous elements, but with one special rule: whenever the cumulative sum becomes exactly 0, the next element starts a new running total from 0. The function should return a list of the same length as `nums`.

Constraints

- `nums` may be empty. - The length of `nums` is at most 10^5. - Each integer in `nums` is in the range [-1000, 1000].

Example

>>> running_total_window([1, 2, 3])
[1, 3, 6]
>>> running_total_window([1, -1, 2])
[1, 0, 2]
>>> running_total_window([1, -1, 0, 3, -3, 5])
[1, 0, 0, 3, 0, 5]
>>> running_total_window([])
[]
8 points ~10 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Keep a variable for the current running total.
Reset the running total to zero when it becomes exactly zero.
In the case of empty input, return an empty list.
Think about what to do when the running total hits zero multiple times.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.