Share my Python code, O(n), find start and end for the subarray


  • 0
    W
    class Solution(object):
        def minSubArrayLen(self, s, nums):
            """
            :type s: int
            :type nums: List[int]
            :rtype: int
            """
            sumx,start,min_l=0,0,len(nums)+1
            for i in range(len(nums)):
                sumx+=nums[i]
                while sumx>=s:
                    min_l=min(min_l,i-start+1)
                    sumx-=nums[start]
                    start+=1
            return 0 if min_l==len(nums)+1 else min_l

Log in to reply
 

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