Anas Dew
Posted on December 18, 2022
Hey dev! In case you don't know, I've started #45daysofleetcode and in these 45 days i'll be writing about two problems every day.
The problem, how I solved it and a bit of detailed explanation.
I'm sharing this progress with you so that you too can learn a bit of different perspective and have new solutions so hit like to join this journey.
I've chosen the best list of problems from medium to hard. That made lots of developers crack the interview.
Are you excited?
Today's Problem
Name
Running sum of 1d array
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.
Example
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].
My Approach
Simply add first value of original array to running array, and then start a loop from 1.
With each iteration, append (last element + current element) to the running array. then return it.
Code
class Solution:
def runningSum(self, nums: List[int]) -> List[int]:
runningArray = [nums[0]]
for i in range(1, len(nums)):
runningArray.append(runningArray[i-1]+nums[i])
return runningArray
Let me know if you've any questions or suggestions!.
If you want full list of solutions, here's the list. Make sure you star the repository.
Follow to join this #45daysofleetcode
Bye!
Posted on December 18, 2022
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.