Iam_Sandeep

209. Minimum Size Subarray Sum

Jun 16th, 2022
1,100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. '''
  2. The result is initialized as res = n + 1.
  3. One pass, remove the value from sum s by doing s -= A[j].
  4. If s <= 0, it means the total sum of A[i] + ... + A[j] >= sum that we want.
  5. Then we update the res = min(res, j - i + 1)
  6. Finally we return the result res
  7. '''
  8.  def minSubArrayLen(self, s, A):
  9.         i, res = 0, len(A) + 1
  10.         for j in xrange(len(A)):
  11.             s -= A[j]
  12.             while s <= 0:
  13.                 res = min(res, j - i + 1)
  14.                 s += A[i]
  15.                 i += 1
  16.         return res if res!=n+1 else 0
Advertisement
Add Comment
Please, Sign In to add comment