code.ashish.me

Atom feed

Recently added: 128 Longest Consecutive Sequence, 347 Top K Frequent Elements, 045 Jump Game 2, 228 Summary Ranges, 219 Contains Duplicate 2

1480 Running Sum Of 1D Array

/**
 *
 * Ashish Patel
 * e: ashishsushilPatel@gmail.com
 * w: https://ashish.me
 *
 */

function TreeNode(val) {
  this.val = val
  this.left = this.right = null
}

const runningSumOf1DArray = nums => {
  let result = []
  const helper = (nums, index, sum) => {
    if (nums.length == index) {
      return
    }
    result[index] = sum + nums[index]
    helper(nums, index + 1, sum + nums[index])
  }
  helper(nums, 0, 0)
  return result
}

test('runningSumOf1DArray', () => {
  expect(runningSumOf1DArray([1, 2, 3, 4])).toEqual([1, 3, 6, 10])
})

Created 2021-01-31T09:12:13+05:30, updated 2021-01-31T10:03:49+05:30 · History · Edit