Given an array of positive integers nums and a positive integer target, return the minimal length of a subarray whose sum is greater than or equal to target. If there is no such subarray, return 0 instead. Assume 1 <= nums[i].
# Time complexity: O(n)# Space complexity: O(1)defminSubArrayLen(target:int, nums: List[int])->int: start = end = currSum =0 answer =float("inf")while end <len(nums):# Expand window size from end currSum += nums[end] end +=1# Decrease window size from startwhile currSum >= target: answer =min(answer, end - start) currSum -= nums[start] start +=1return0if answer ==float("inf")else answer
Given a string s, find the length of the longest substring without repeating characters.
# Time complexity: O(n)# Space complexity: O(n)deflengthOfLongestSubstring(s:str)->int: answer = start = end =0 m ={}# char : indexwhile end <len(s):# Decrease window size to exclude duplicateif s[end]in m: start =max(start, m[s[end]]+1) m[s[end]]= end
end +=1 answer =max(answer, end - start)return answer