LeetCode: Running Sum Of 1d Array Solution
AccumulateApproach
Either mutate array by accumulate sum or calculate prefix sum
Implementation
1// approach 1: mutate the array2var runningSum = function (nums) {3 for (let i = 1; i < nums.length; i++) {4 nums[i] += nums[i - 1]5 }6 return nums7}89// approach 2: prefix sum10var runningSum = function (nums) {11 const prefixSums = []12 let sum = 013 for (const num of nums) {14 sum += num15 prefixSums.push(sum)16 }17 return prefixSums18}
References
Comments
Loading comments...
Tags
leetcode
array
Apply and earn a $2,500 bonus once you're hired on your first job!
Clients from the Fortune 500 to Silicon Valley startups
Choose your own rate, get paid on time
From hourly, part-time, to full-time positions
Flexible remote working environment
A lot of open JavaScript jobs!!
Fact corner: Referred talent are 5x more likely to pass the Toptal screening process than the average applicant.
Still hesitate? Read HoningJS author's guide on dealing with Toptal interview process.