fork download
  1. package main
  2. import "fmt"
  3. func min (a,b int) int{
  4. if a < b {return a}
  5. return b
  6. }
  7. func minSubArrayLen(target int, n []int) int {
  8. l:=0
  9. sum:=0
  10. m:=1000000
  11. for r := range n {
  12. fmt.Printf("r %d s %d l %d m %d\n", r, sum, l, m)
  13. sum += n[r]
  14. if sum <= target {
  15. continue
  16. }
  17. for {
  18. if sum >= target {
  19. m = min(m, r-l+1)
  20. } else {
  21. break
  22. }
  23. sum -= n[l]
  24. l++
  25. if l > r {
  26. break
  27. }
  28. }
  29. }
  30.  
  31. return m
  32. }
  33.  
  34. func main(){
  35. minSubArrayLen(7, []int{2,3,1,2,4,3})
  36. }
Success #stdin #stdout 0s 5288KB
stdin
Standard input is empty
stdout
r 0 s 0 l 0 m 1000000
r 1 s 2 l 0 m 1000000
r 2 s 5 l 0 m 1000000
r 3 s 6 l 0 m 1000000
r 4 s 6 l 1 m 4
r 5 s 6 l 3 m 3