滑动窗口(2)——不定长

python 复制代码
class Solution:
    def lengthOfLongestSubstring(self, s: str) -> int:
        cnt = defaultdict(int)
        left = res = 0
        for i, e in enumerate(s):
            cnt[e] += 1
            while cnt[e] > 1:
                cnt[s[left]] -= 1
                left += 1
            res = max(res, i - left + 1)
        return res
        
python 复制代码
class Solution:
    def maximumLengthSubstring(self, s: str) -> int:
        cnt = defaultdict(int)
        left = ans = 0
        for right, e in enumerate(s):
            cnt[e] += 1
            while cnt[e] > 2:
                cnt[s[left]] -= 1
                left += 1
            ans = max(ans, right - left + 1)
        return ans
python 复制代码
class Solution:
    def longestSubarray(self, nums: List[int]) -> int:
        cnt_0 = 0
        left = ans = 0
        for right, e in enumerate(nums):
            if e == 0:
                cnt_0 += 1
            while cnt_0 > 1:
                if nums[left] == 0:
                    cnt_0 -= 1
                left += 1
            ans = max(ans, right - left)
        
        return ans
python 复制代码
class Solution:
    def minRemoval(self, nums: List[int], k: int) -> int:
        nums.sort()
        left = ans = 0
        for i, e in enumerate(nums):
            while nums[left] * k < e:
                left += 1
            ans = max(ans, i - left + 1)
        return len(nums) - ans
python 复制代码
class Solution:
    def equalSubstring(self, s: str, t: str, maxCost: int) -> int:
        cnt = 0
        ans = left = 0
        for i in range(len(s)):
            cnt += abs(ord(s[i]) - ord(t[i]))       
            while cnt > maxCost:
                cnt -= abs(ord(s[left]) - ord(t[left]))
                left += 1
            ans = max(ans, i - left + 1)
        return ans
python 复制代码
class Solution:
    def totalFruit(self, fruits: List[int]) -> int:
        cnt = defaultdict(int)
        left = ans = 0
        for i, e in enumerate(fruits):
            cnt[e] += 1
            while len(cnt) > 2:
                out = fruits[left]
                cnt[out] -= 1
                if cnt[out] == 0:
                    del cnt[out]
                left += 1
            ans = max(ans, i - left + 1)
        return ans
        
python 复制代码
class Solution:
    def maximumUniqueSubarray(self, nums: List[int]) -> int:
        cnt = defaultdict(int)
        left = ans = tmp = 0
        for i, e in enumerate(nums):
            cnt[e] += 1
            tmp += e
            while cnt[e] > 1:
                out = nums[left]
                cnt[out] -= 1
                left += 1
                tmp -= out
            ans = max(ans, tmp)
        return ans
python 复制代码
class Solution:
    def fun(self, nums, goal):
        s = cnt = left =  0
        for i, e in enumerate(nums):
            s += e
            while s >= goal and left <= i:
                out = nums[left]
                s -= out
                left += 1
            cnt += left
        return cnt
    def numSubarraysWithSum(self, nums: List[int], goal: int) -> int:
        upper = self.fun(nums, goal)
        lower = self.fun(nums, goal + 1)
        return upper - lower
        

答案就是元素和≥k的子数组个数,减去元素和≥k + 1的子数组个数。

python 复制代码
class Solution:
    def numSubarrayProductLessThanK(self, nums: List[int], k: int) -> int:
        if k <= 1:
            return 0
        left = cnt = 0
        tmp = 1
        for i, e in enumerate(nums):
            tmp *= e
            while tmp >= k:
                tmp //= nums[left]
                left += 1
            cnt += i - left + 1
        return cnt

这个注意的是left和right中间的都算

python 复制代码
class Solution:
    def numberOfSubstrings(self, s: str) -> int:
        cnt = defaultdict(int)
        left = ans = 0
        for i, e in enumerate(s):
            cnt[e] += 1
            while len(cnt) == 3:
                out = s[left]
                cnt[out] -= 1
                left += 1
                if cnt[out] == 0:
                    del cnt[out]
            ans += left
        return ans
        
python 复制代码
class Solution:
    def maxConsecutiveAnswers(self, answerKey: str, k: int) -> int:
        cnt = defaultdict(int)
        ans = left = 0
        for i, e in enumerate(answerKey):
            cnt[e] += 1
            while cnt['T'] > k and cnt['F'] > k:
                out = answerKey[left]
                cnt[out] -= 1
                left += 1
            ans = max(ans, i - left + 1)
        return ans

如果 T 和 F 的出现次数都超过 k,就移动左端点 left,直到 T 和 F 的出现次数至少有一个 ≤k

相关推荐
apocelipes1 天前
golang unique包和字符串内部化
java·python·性能优化·golang
Geoking.1 天前
NumPy zeros() 函数详解
python·numpy
Full Stack Developme1 天前
java.text 包详解
java·开发语言·python
丁浩6661 天前
Python机器学习---2.算法:逻辑回归
python·算法·机器学习
B站_计算机毕业设计之家1 天前
计算机毕业设计:Python农业数据可视化分析系统 气象数据 农业生产 粮食数据 播种数据 爬虫 Django框架 天气数据 降水量(源码+文档)✅
大数据·爬虫·python·机器学习·信息可视化·课程设计·农业
Q_Q5110082851 天前
python+uniapp基于微信小程序的旅游信息系统
spring boot·python·微信小程序·django·flask·uni-app·node.js
伏小白白白1 天前
【论文精度-2】求解车辆路径问题的神经组合优化算法:综合展望(Yubin Xiao,2025)
人工智能·算法·机器学习
鄃鳕1 天前
python迭代器解包【python】
开发语言·python
无敌最俊朗@1 天前
数组-力扣hot56-合并区间
数据结构·算法·leetcode
懷淰メ1 天前
python3GUI--模仿百度网盘的本地文件管理器 By:PyQt5(详细分享)
开发语言·python·pyqt·文件管理·百度云·百度网盘·ui设计