Python (Two Pointers) (beat over 99.2%) optimization


  • 0
    J

    optimization version of available solution: reduce the number of times to use min()

    def minSubArrayLen(self, s, nums):
            """
            :type s: int
            :type nums: List[int]
            :rtype: int
            """
            if((not nums) or s < 1):
                return 0
            min_len = len(nums) + 1
            cur_sum = 0
            j = 0
            for i in xrange(len(nums)):
                cur_sum += nums[i]
                if(cur_sum < s):
                    continue
                while(cur_sum >= s):
                    cur_sum -= nums[j]
                    j += 1
                min_len = min(min_len, i - j + 2)
            return min_len if min_len < len(nums) + 1 else 0
    

Log in to reply
 

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