Leecode 0209. Minimum Size Subarray Sum
209. Minimum Size Subarray Sum
题目
Given an array of n positive integers and a positive integer s, find the minimal length of a contiguous subarray of which the sum ≥ s. If there isn't one, return 0 instead.
Example 1:
1 | Input: s = 7, nums = [2,3,1,2,4,3] |
Follow up:
If you have figured out the O(n) solution, try coding another solution of which the time complexity is O(n log n).
解题思路
这一题的解题思路是用滑动窗口。在滑动窗口 [i,j]之间不断往后移动,如果总和小于 s,就扩大右边界 j,不断加入右边的值,直到 sum > s,之和再缩小 i 的左边界,不断缩小直到 sum < s,这时候右边界又可以往右移动。以此类推。
解法1:滑动窗口
1 |
|
- 初始化:
left
指针起始位置为 0sum
用于累计当前窗口的元素和min_len
初始化为INT_MAX
,用于记录最小长度
- 扩大窗口:
right
指针从 0 开始遍历数组- 每次将
nums[right]
加入sum
- 缩小窗口:
- 当
sum >= target
时,进入循环尝试缩小窗口 - 计算当前窗口长度并更新
min_len
- 从
sum
中减去nums[left]
并将left
右移,缩小窗口
- 当
- 结果处理:
- 如果
min_len
仍为INT_MAX
,说明没有找到符合条件的子数组,返回 0 - 否则返回
min_len
- 如果
复杂度分析
- 时间复杂度:O (n),其中 n 是数组长度。每个元素最多被
left
和right
指针访问一次 - 空间复杂度:O (1),只使用了常数个额外变量
解法2:滑动窗口优化
1 | class Solution { |
核心逻辑解析
- 变量定义:
i
:窗口左边界j
:窗口右边界s
:当前窗口的元素总和xiao
:记录最小子数组长度,初始化为INT_MAX
- 算法流程:
- 外层循环移动右边界
j
,不断扩大窗口并累加总和s
- 当
s >= target
时,进入内层循环尝试缩小左边界 - 每次缩小左边界时更新最小长度,并调整总和
s
- 最终根据
xiao
的值判断是否存在符合条件的子数组
- 外层循环移动右边界
All articles on this blog are licensed under CC BY-NC-SA 4.0 unless otherwise stated.