LeetCode Daily Question 6
3 月 11 日 每日一题
题目:209. Minimum Size Subarray Sum
Given an array of positive integers nums and a positive integer target, return the minimal length of a contiguous subarray [numsl, numsl+1, …, numsr-1, numsr] of which the sum is greater than or equal to target. If there is no such subarray, return 0 instead.
eg:
1 | Input: target = 7, nums = [2,3,1,2,4,3] |
思路
Sliding Window
本题最快且最容易理解的写法是使用 sliding window。
建立双指针 left 和 right,移动 right 对 total 进行累加。如果 total 大于等于 target,则触发内部 while 循环,调整 res 之后移动 left, total 减去 left 指向的 value。继续移动 right,直到 right 遍历整个数组。时间复杂度为 O(n)。
Binary Search
使用 Binary Search 写法很有趣。
首先新建数组 total[i],total 数组为升序。
$total[0] = 0$
$total[i] = nums[0] + nums[1] + … nums[i-1]$
对于每一个 i,利用 Binary Search 找到第一个 j ,满足 j < i 并且 total[j] > total[i] - target。并且易得 total[j - 1] <= total[i] - target
$total[j] > total[i] - target$
则 $nums[0] + nums[1] + nums[j - 1] > nums[0] + nums[1] + nums[i - 1] - target$
$total[j - 1] <= total[i] - target$
则 $nums[0] + nums[1] + … + nums[j - 2] <= nums[0] + nums[1] + … + nums[i - 1] - target$
化简后可得
$target > nums[j] + … +nums[i - 1]$
$target <= nums[j - 1] + … nums[i - 1]$
这意味着对于当前的 i - 1,j - 1 到 i - 1 是最短的满足要求的 subarray。再遍历所有可能的 i 之后,就能找到最短的 subarray。
时间复杂度为 O(nlogn) for * binary search
今天一题顶两题,算是补上了昨天的…
代码
1 | //sliding window |