灵神题单:据说很好的题单,mark一下
分享丨【算法题单】滑动窗口与双指针(定长/不定长/单序列/双序列/三指针/分组循环) - 讨论 - 力扣(LeetCode)
LeetCode 209 长度最小的子数组
问题分析:
给定一个只包含正数的数组和一个目标值 `target`,我们需要找到和大于等于 `target` 的最短子数组长度。
暴力解法:
最直接的方法是 固定左端点,枚举右端点,并在过程中计算子数组和:
- 以第 0 个元素为左端点时,枚举子数组 `a, ab, abc, abcd, abcde`;
- 以第 1 个元素为左端点时,枚举 `b, bc, bcd, bcde`;
- 依此类推直到末尾。
每个子数组求和后,若 sum ≥ target,就用当前长度更新最小值 ans。
时间复杂度:O(n²)。
第一次优化:
由于数组中元素都是正数,如果从某个左端点开始,当累加到 `a+b+c < target`,继续加 `d` 后得到 `a+b+c+d ≥ target`,那么再加 `e` 一定也满足 `≥ target`。因此,当首次达到 `≥ target` 时就可以 提前 break,不需要继续枚举更长的子数组。
时间复杂度仍为 O(n²),但减少了部分冗余。
最终优化(滑动窗口 / 双指针):
进一步利用"正数数组"的 **单调性**。
- 假设从左端点 `a` 开始,`a+b+c < target` 且 `a+b+c+d ≥ target`,则以 `a` 为起点的最短子数组长度为 4。
- 接着考虑左端点为 `b`,由于 `a+b+c < target`,则 `b` 和 `b+c` 一定也 `< target`,因此无需从 b 重新累加,可以直接从上一次窗口 `a+b+c+d` 的和中减去 `a` 继续。
- 如果 `b+c+d ≥ target`,则以 `b` 为起点的最短子数组长度为 3;否则继续扩展右指针,直到没有新元素可加入。
这样每个元素最多被访问两次(一次进窗口,一次出窗口),时间复杂度优化到 O(n)。
这个版本有的会超时,可能因为sum那里的复杂度问题
python
class Solution(object):
def minSubArrayLen(self, target, nums):
"""
:type target: int
:type nums: List[int]
:rtype: int
"""
length=float('inf')
slow,fast=0,0
while fast<len(nums):
while(sum(nums[slow:fast+1])<target):
fast=fast+1
if fast==len(nums):
return 0 if length==float('inf') else length
length=min(length,fast-slow+1)
slow=slow+1
if length==float('inf'):
return 0
else:
return length
这个可以AC,total+=nums[fast]的时候要注意先return再加,否则可能边界超出
python
class Solution(object):
def minSubArrayLen(self, target, nums):
"""
:type target: int
:type nums: List[int]
:rtype: int
"""
length=float('inf')
slow,fast=0,0
total=nums[fast]
while fast<len(nums):
while(total<target):
fast=fast+1
if fast==len(nums):
return 0 if length==float('inf') else length
total+=nums[fast]
total-=nums[slow]
length=min(length,fast-slow+1)
slow=slow+1
if length==float('inf'):
return 0
else:
return length