Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- '''
- The result is initialized as res = n + 1.
- One pass, remove the value from sum s by doing s -= A[j].
- If s <= 0, it means the total sum of A[i] + ... + A[j] >= sum that we want.
- Then we update the res = min(res, j - i + 1)
- Finally we return the result res
- '''
- def minSubArrayLen(self, s, A):
- i, res = 0, len(A) + 1
- for j in xrange(len(A)):
- s -= A[j]
- while s <= 0:
- res = min(res, j - i + 1)
- s += A[i]
- i += 1
- return res if res!=n+1 else 0
Advertisement
Add Comment
Please, Sign In to add comment