滑动窗口(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

相关推荐
Dillon Dong2 小时前
【风电控制】TI TMS320F28379D 双CPU架构解析与任务分布设计
嵌入式硬件·算法·变流器·风电控制
花酒锄作田5 小时前
[python]argparse 包在聊天机器人中的应用
python
NiceCloud喜云7 小时前
Opus 4.8 的 Effort Control 怎么选:Low 到 Max 五档策略
android·java·大数据·前端·c++·python·spring
小羊在睡觉7 小时前
力扣84. 柱状图中最大的矩形
后端·算法·leetcode·golang·go
3DVisionary8 小时前
蓝光三维扫描:医疗制造的精度焦虑怎么解
人工智能·算法·制造·蓝光三维扫描·医疗制造·三维检测·义齿检测
AI玫瑰助手8 小时前
Python函数:默认参数的定义与注意事项
开发语言·python·信息可视化
好评笔记8 小时前
机器学习面试八股——常用损失函数
人工智能·深度学习·算法·机器学习·校招
weixin_468466858 小时前
全局与局部注意力机制新手实战指南
人工智能·python·深度学习·算法·自然语言处理·transformer·注意力机制
小糖学代码8 小时前
LLM系列:环境搭建:5.Python-dotenv 环境变量管理
人工智能·python·深度学习·神经网络
sheeta19988 小时前
LeetCode 每日一题笔记 日期:2026.05.29 题目:3300. 最小元素
笔记·leetcode