Skip to content

Commit

Permalink
Merge pull request neetcode-gh#1910 from josuebrunel/feat/0209-minimu…
Browse files Browse the repository at this point in the history
…m-size-subarray-sum.go

Create 0209-minimum-size-subarray-sum.go
  • Loading branch information
tahsintunan authored Jan 8, 2023
2 parents 9ef54ae + a3a5064 commit 48fa3e3
Showing 1 changed file with 25 additions and 0 deletions.
25 changes: 25 additions & 0 deletions go/0209-minimum-size-subarray-sum.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/*
Time: O(n)
Space: O(1)
*/

func minSubArrayLen(target int, nums []int) int {
minSize := len(nums) + 1
L := 0
curSum := 0

for R := range nums {
curSum += nums[R]
for curSum >= target {
if R+1-L < minSize {
minSize = R + 1 - L
}
curSum -= nums[L]
L += 1
}
}
if minSize == len(nums)+1 {
return 0
}
return minSize
}

0 comments on commit 48fa3e3

Please sign in to comment.