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

02 Minimum Size Subarray Sum

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

const minimumSizeSubarraySum = (nums, target) => {
  let left = 0
  let total = 0
  let result = Infinity
  for (let right = 0; right < nums.length; right++) {
    total += nums[right]
    while (total >= target) {
      result = Math.min(result, right - left + 1)
      total -= nums[left]
      left += 1
    }
  }
  return result
}

test('minimumSizeSubarraySum', () => {
  expect(minimumSizeSubarraySum([2, 3, 1, 2, 4, 3], 7)).toEqual(2)
})

Created 2022-02-04T04:03:39+00:00 · Edit