NLeetCode Solutions
ProblemsPatternsStatistics
© 2026 Created by Trịnh Minh Nhật
GitHubLeetCode
Related posts
  • 238. Product of Array Except SelfMedium
  • 746. Min Cost Climbing StairsEasy
  • 2239. Find Closest Number to ZeroEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy

1480. Running Sum of 1d Array

Easy
Mid LevelArrayPrefix SumWeekly Contest 193
Answered: Nov 15, 2022
View on LeetCode

Problem Description

Given an array nums. We define a running sum of an array as runningSum[i] = sum(nums[0]…nums[i]).

Return the running sum of nums.

Examples

Example 1

Input: nums = [1,2,3,4]
Output: [1,3,6,10]
Explanation:
Running sum is obtained as follows: [1, 1+2, 1+2+3, 1+2+3+4].

Example 2

Input: nums = [1,1,1,1,1]
Output: [1,2,3,4,5]
Explanation:
Running sum is obtained as follows: [1, 1+1, 1+1+1, 1+1+1+1, 1+1+1+1+1].

Example 3

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

Constraints

  • 1 <= nums.length <= 1000
  • -10^6 <= nums[i] <= 10^6

💡 Hints (1)


Solutions

Complexity Analysis

Time Complexity:O(n)

The algorithm iterates through the array once, so the time complexity is linear in the size of the input array.

Space Complexity:O(1)

The algorithm uses only a constant amount of extra space, regardless of the input size.

In this approach, we iterate through the input array nums and maintain a variable sum that keeps track of the running sum up to the current index. For each element in the array, we add its value to sum and then update the element in the array to be the current value of sum. This way, by the end of the loop, each element in nums will have been replaced by the running sum up to that index.

Complexity Analysis

Time Complexity:O(n)

The algorithm iterates through the array once, so the time complexity is linear in the size of the input array.

Space Complexity:O(1)

The algorithm uses only a constant amount of extra space, regardless of the input size.

In this approach, we start iterating from the second element of the array (index 1) and update each element to be the sum of itself and the previous element. This way, each element at index i will become the running sum up to that index by adding the value of the previous element (which already contains the running sum up to index i-1) to its own value.

Conclusion

ApproachRatingTime ComplexitySpace ComplexityAdvantagesDisadvantages
Prefix Sum with Temporary Variable
O(n)O(n)O(n)O(1)O(1)O(1)Explicit running sum logic with a separate variableSlightly more verbose with an extra variable
Prefix Sum In-place from Second Element
O(n)O(n)O(n)O(1)O(1)O(1)Most concise — directly updates each element using the previous one, no extra variable neededNone

The optimal approach for this problem is Solution 2: Prefix Sum In-place from Second Element, which achieves O(n)O(n)O(n) time complexity and O(1)O(1)O(1) space complexity. It iterates from the second element and simply adds the previous element's value, making it the most elegant and concise solution.


Previous746. Min Cost Climbing Stairs
Next2239. Find Closest Number to Zero
Related posts
  • 238. Product of Array Except SelfMedium
  • 746. Min Cost Climbing StairsEasy
  • 2239. Find Closest Number to ZeroEasy
  • 1. Two SumEasy
  • 14. Longest Common PrefixEasy
On this page
14 lines
1class Solution:
2 def runningSum(self, nums: List[int]) -> List[int]:
3 if len(nums) > 1000 or len(nums) < 1:
4 raise ValueError("The length of nums must be between 1 and 1000.")
5
6 # Initialize a variable to keep track of the running sum
7 sum = 0
8
9 # Iterate through the input array and update each element to be the running sum up to that index
10 for i in range(len(nums)):
11 sum += nums[i]
12 nums[i] = sum
13
14 return nums
10 lines
1class Solution:
2 def runningSum(self, nums: List[int]) -> List[int]:
3 if len(nums) > 1000 or len(nums) < 1:
4 raise ValueError("The length of nums must be between 1 and 1000.")
5
6 # Iterate through the array starting from the second element and update each element to be the sum of itself and the previous element
7 for i in range(1, len(nums)):
8 nums[i] += nums[i - 1]
9
10 return nums