Skip to content

Commit

Permalink
O(N)解法
Browse files Browse the repository at this point in the history
  • Loading branch information
rinwf committed Oct 1, 2018
1 parent 0334bbc commit cc1e9c2
Showing 1 changed file with 14 additions and 33 deletions.
47 changes: 14 additions & 33 deletions lc209. Minimum Size Subarray Sum.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,21 @@ def minSubArrayLen(self, s, nums):
:type nums: List[int]
:rtype: int
"""
def bin_search(left, right, key, sums):
while left <= right:
mid = (left + right) // 2
if sums[mid] >= key:
right = mid - 1
else:
left = mid + 1
return left
left = right = su = 0
res = len(nums) + 1
while right < len(nums):
su += nums[right]
right += 1
if su >= s:
while su >= s:
if right - left < res:
res = right - left
su -= nums[left]
left += 1
return 0 if res == len(nums) + 1 else res

length = len(nums)
sums = [None] * (length + 1)
sums[0] = 0

for i in range(0, length):
sums[i + 1] = sums[i] + nums[i]

# 两层循环 O(N^2)解法
# for i in range(1, length + 1):
# for j in range(length + 1 - i):
# if sums[j + i] - sums[j] >= s:
# return i

res = length + 1
for i in range(length + 1):
# sums[right]-sums[left],不包括nums[left]
tem = bin_search(i + 1, length, sums[i] + s, sums)
if tem == length + 1:
break
if tem - i < res:
res = tem - i

return 0 if res == length + 1 else res

s=100
nums=[]
s = 7
nums = [2, 3, 1, 2, 4, 3]
soln = Solution()
print(soln.minSubArrayLen(s, nums))

0 comments on commit cc1e9c2

Please sign in to comment.