Problem List

Minimum Size Subarray Sum

May 31, 2025Go array, sliding window, prefix summedium

Problem

Approach

Reflections

This problem reinforced the power of the sliding window technique when dealing with contiguous subarrays and sum conditions. In retrospect, I overcomplicated earlier attempts by creating new slices for every window—using an integer variable to track the sum inline is way more efficient.

Performance

Complexity

Go Solution
func minSubArrayLen(target int, nums []int) int {
    left, right := 0,0
    n := len(nums)
    candidate := 0
    sum := 0

    for right <= n {
        window := nums[left:right]

        if sum < target {
            if right <= n - 1{
                sum += nums[right]
            }
            right++
        } else if sum >= target {
            candidate = getCandidate(candidate,len(window))
            sum -= nums[left]
            left++
        }
    }

    return candidate
}

func getCandidate(a, b int) int {
    if a == 0 {
        return b
    }

    if a < b {
        return a
    } 

    return b
}
LeetCode Problem Link