Thursday, August 27, 2015

Leetcode 209. Minimum Size Subarray Sum

https://leetcode.com/problems/minimum-size-subarray-sum/

Rolling window method.

Solution:
# T:O(n) S:O(1)
class Solution:
    # @param {integer} s
    # @param {integer[]} nums
    # @return {integer}
    def minSubArrayLen(self, s, nums):
        start, sum, mid_len = 0, 0, float("inf")
        for i in xrange(len(nums)):
            sum += nums[i]
            while sum >= s:
                min_len = min(min_len, i - start + 1)
                sum -= nums[start]
                start += 1
        if min_len == float("inf"):
            return 0
        return min_len
Run Time: 88 ms

No comments:

Post a Comment