You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
"""[start] 12:34Problem Solving Thought Process:1. Brute ForceUsing 2 for loops to find out 2 element that sums up to the targetConstantly update the minimum length in a globalMinglobalMin = infifor i -> n for j -> n cur_sum += nums[j] if cur_sum > target: globalMin = min(globalMin, cur_sum) breakreturn globalMinTime: O(N^2)Space: O(1)2. Two Pointer We can potentially only iterate "j" from beginning to end once by moving "i" along the wayTime: O(N)Space: O(1)"""#[Coding Start] 12:39#[Coding Finish] 12:43#[Eyeballing code] 12:43 - 12:46# Bug"""globalMin = float('inf')curSum = 0j = 0for i in range(len(nums)): while j < len(nums) and curSum < target: curSum += nums[j] j += 1 if curSum >= target: globalMin = min(globalMin, j - i) curSum -= nums[i]return globalMin -> bug1. 如果没有找到合适的结果,应该返回0而不是初始值的infinitefail testcases:3[1,1]"""classSolution(object):
defminSubArrayLen(self, target, nums):
globalMin=float('inf')
curSum=0j=0foriinrange(len(nums)):
whilej<len(nums) andcurSum<target:
curSum+=nums[j]
j+=1ifcurSum>=target:
globalMin=min(globalMin, j-i)
curSum-=nums[i]
returnglobalMinifglobalMin!=float('inf') else0
The text was updated successfully, but these errors were encountered:
209. Minimum Size Subarray Sum
公瑾回来刷题咯
The text was updated successfully, but these errors were encountered: