clean Golang O(n) solution


  • 0
    D

    idea is simple: use two pointers 'start' and 'end' to track the moving window.

    func minSubArrayLen(s int, nums []int) int {
        n := len(nums)
        minLen,cnt,curLen := n+1,0,0
        for start,end := 0,0 ; end<n ; end++ {
            cnt += nums[end]
            for cnt >= s {
                curLen = end-start+1
                if curLen < minLen {
                    minLen = curLen
                }
                cnt -= nums[start]
                start++
            }
        }
        if minLen==n+1 {
            return 0
        }
        return minLen
    }
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.