code.ashish.me

Atom feed

Recently added: 02 Count Of Subset Sum, 416 Partition Equal Subset Sum, 01 Subset Sum, 518 Coin Change 2, 983 Minimum Cost For Tickets

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:00:13+05:18, updated 2021-01-31T09:51:49+05:18 · History · Edit