algorythms
Sliding Window
LC #209Medium

Minimum Size Subarray Sum

Sliding Window
AmazonGoogleMeta

Problem

Given an array of positive integers and a target, find the minimal length of a subarray whose sum is ≥ target.

arraysliding-windowprefix-sum

Constraints

  • 1 ≤ n ≤ 10⁵
  • 1 ≤ target ≤ 10⁹
  • 1 ≤ nums[i] ≤ 10⁴

Example

Inputtarget = 7, nums = [2, 3, 1, 2, 4, 3]
Output2
Why

[4, 3] has sum ≥ 7 with minimum length 2

Hints — reveal one at a time