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_lenRun Time: 88 ms
No comments:
Post a Comment