Minimum Size Subarray Sum
Problem statement
nums holds positive integers. Find the shortest contiguous window whose sum is at least target. If no window works, return 0.
Example:
Input:
nums = [2, 3, 1, 2, 4, 3], target = 7
Expected output:
2
Why: [4, 3] is a shortest window that sums to 7.
Practice on LeetCode: Minimum Size Subarray Sum
Golang Solution
func minSubArrayLen(target int, nums []int) int {
if len(nums) == 0 {
return 0
}
i, j := 0, 0
minLen := 99999999999
tempSum := 0
for j < len(nums) {
tempSum += nums[j]
for tempSum >= target {
if minLen > j - i + 1 {
minLen = j - i + 1
}
tempSum -= nums[i]
i += 1
}
j += 1
}
if minLen == 99999999999 {
minLen = 0
}
return minLen
}
