Skip to content

Latest commit

 

History

History
45 lines (36 loc) · 940 Bytes

1480_runningSumArr.md

File metadata and controls

45 lines (36 loc) · 940 Bytes

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.

O(n) Time and O(n) Space

  • Create a sum variable to store current sum in it ans push back it in the result array.

Code

class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        int sum = 0;
        vector<int> ans;
        int n = nums.size();
        for(int i=0;i<n;i++){
            sum+=nums[i];
            ans.push_back(sum);
        }
        return ans;
    }
};

O(n) Time and O(1) Space

  • We can modify input array directy

Code

class Solution {
public:
    vector<int> runningSum(vector<int>& nums) {
        int n = nums.size();
        for(int i = 1;i<n;i++){
            nums[i]+=nums[i-1];
        }
        return nums;
    }
};