AceYourInterviews
Checking sign-in…

26. Minimum Size Subarray Sum — Positive Inputs

IntermediatePositive-sum threshold window

Find the shortest nonempty continuous part of the array whose sum is at least the target. You are minimizing the number of included positions, rather than finding the smallest qualifying sum.

The positive-input restriction is part of this problem. An approach that depends on positivity cannot be reused unchanged when zeros or negative numbers are allowed.

Example 1

Input

target=7, nums=[2,3,1,2,4,3]

Output

2

Explanation: The final [4,3] reaches seven in two positions.

Example 2

Input

target=4294967294, nums=[2147483647,2147483647]

Output

2

Explanation: The sum exceeds int range but is exact as long.

Constraints

  • 0 <= nums.length <= 105
  • 1 <= nums[i] <= 231 - 1
  • 1 <= target <= 263 - 1
Hints
Hint 1

What does strict positivity guarantee about removing the left endpoint?

Hint 2

When a window qualifies, can its shorter suffix still qualify?

Hint 3

Record each valid length and shrink until the sum drops below target.

Practice on LeetCode ↗
Code
Saved on this deviceLn 1, Col 1

Shrink several qualifying suffixes

7
[2,3,1,2,4,3]
2