力扣长度最小的子数组
// 长度最小的子数组
package slidingwindow
import "math"
/*
给定一个含有 n 个正整数的数组和一个正整数 target 。
找出该数组中满足其总和大于等于 target 的长度最小的 连续
子数组
[numsl, numsl+1, ..., numsr-1, numsr] ,并返回其长度。如果不存在符合条件的子数组,返回 0 。
示例 1:
输入:target = 7, nums = [2,3,1,2,4,3]
输出:2
解释:子数组 [4,3] 是该条件下的长度最小的子数组。
示例 2:
输入:target = 4, nums = [1,4,4]
输出:1
示例 3:
输入:target = 11, nums = [1,1,1,1,1,1,1,1]
输出:0
*/
// leecode官方 滑动窗口
func MinSubArrayLen1(target int, nums []int) int {
length := len(nums)
if length == 0 {
return 0
}
ans := math.MaxInt32
start, end := 0, 0
sum := 0
for end < length {
sum += nums[end]
for sum >= target {
ans = min(ans, end-start+1)
sum -= nums[start]
start++
}
end++
}
if ans == math.MaxInt32 {
return 0
}
return ans
}
func min(x, y int) int {
if x < y {
return x
}
return y
}