Skip to content

Instantly share code, notes, and snippets.

@bparanj
Created August 18, 2020 20:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bparanj/03ca8f5a759e8bb5f2f01b4070fafb7c to your computer and use it in GitHub Desktop.
Save bparanj/03ca8f5a759e8bb5f2f01b4070fafb7c to your computer and use it in GitHub Desktop.
# @param {Integer[]} nums
# @return {Integer[]}
def running_sum(nums)
nums.each_with_index do |item, index|
if index == 0
next
end
nums[index] = nums[index] + nums[index-1]
end
end
@bparanj
Copy link
Author

bparanj commented Aug 19, 2020

Cleaner approach:

def running_sum(nums)
  for index in (1..nums.size-1) do
    nums[index] += nums[index-1]  
  end
  nums
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment