very tricky cuz the length of sliding window is not fixed so I didnt know how. I got a hint from gemini and voila but im calculating the sum of sliding window for each for iteration. So there was runtime but it still worked
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
ans=int(10e18)
left,right=0,0
while left< len(nums) and right<len(nums):
window=nums[left:right+1]
if sum(window)>=target:
ans=min(ans,right-left+1)
left+=1
else:
if left==0 and right==len(nums)-1:
return 0
else:
right+=1
return ans
so i searched online and the way the conditions are thought are so good here. They didnt make it like if cur_Sum>=target else. The conditions were if cur_sum>=target, elif right<len(nums) cuz this elif checks if we can add as much elements in the list to cur_sum if cur_sum is smaller than target. If those 2 conditions are not met, (i.e. not a valid sliding window cuz we either have cur_sum smaller than target or the right pointer is out of index), then we break out of this True loop cuz theres no valid ans.
class Solution:
def minSubArrayLen(self, target: int, nums: List[int]) -> int:
ans=int(10e18)
left,right=0,0
cur_sum=0
while True:
if cur_sum>=target:
cur_sum-=nums[left]
ans=min(ans,right-left)
left+=1
elif right<len(nums):
cur_sum+=nums[right]
right+=1
else:
break
if ans==int(10e18):
return 0
else:
return ans
o(n) time
o(1) space