Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sliding Windows #96

Open
tech-cow opened this issue Feb 24, 2020 · 2 comments
Open

Sliding Windows #96

tech-cow opened this issue Feb 24, 2020 · 2 comments

Comments

@tech-cow
Copy link
Owner

No description provided.

@tech-cow
Copy link
Owner Author

209. Minimum Size Subarray Sum

class Solution(object):
    def minSubArrayLen(self, target, nums):      
        globalMin = float('inf')
        curSum = 0
        j = 0
        
        for 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 if globalMin != float('inf') else 0
        

@tech-cow
Copy link
Owner Author

3. Longest Substring Without Repeating Characters

class Solution(object):
    def lengthOfLongestSubstring(self, s):
        globalMax = -float('inf')
        globalSet = set()
        j = 0
        
        for i in range(len(s)):
            while j < len(s) and s[j] not in globalSet:
                globalSet.add(s[j])
                globalMax = max(globalMax, len(globalSet))
                j += 1
            globalSet.remove(s[i])
        
        return globalMax if globalMax != -float('inf') else 0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant