mirror of
https://github.com/halfrost/LeetCode-Go.git
synced 2025-07-06 09:23:19 +08:00
33 lines
441 B
Go
33 lines
441 B
Go
package leetcode
|
|
|
|
func minSubArrayLen(s int, nums []int) int {
|
|
n := len(nums)
|
|
if n == 0 {
|
|
return 0
|
|
}
|
|
left, right, res, sum := 0, -1, n+1, 0
|
|
for left < n {
|
|
if (right+1) < n && sum < s {
|
|
right++
|
|
sum += nums[right]
|
|
} else {
|
|
sum -= nums[left]
|
|
left++
|
|
}
|
|
if sum >= s {
|
|
res = min(res, right-left+1)
|
|
}
|
|
}
|
|
if res == n+1 {
|
|
return 0
|
|
}
|
|
return res
|
|
}
|
|
|
|
func min(a int, b int) int {
|
|
if a > b {
|
|
return b
|
|
}
|
|
return a
|
|
}
|