Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 24s
Classic two-pointer / sliding window: expand right, then shrink
left while the window still satisfies the >= constraint, recording
the smallest valid length.
for r = 0 to n - 1 do
sum := !sum + arr.(r);
while !sum >= target do
... record (r - !l + 1) if smaller ...
sum := !sum - arr.(!l);
l := !l + 1
done
done
For [2; 3; 1; 2; 4; 3], target 7 -> window [4, 3] of length 2.
Sentinel n+1 marks "not found"; final guard reduces to 0.
Tests for + inner while shrinking loop, ref-tracked sum updated
on both expansion and contraction.
194 baseline programs total.
20 lines
439 B
OCaml
20 lines
439 B
OCaml
let min_subarr_sum_at_least arr target =
|
|
let n = Array.length arr in
|
|
let best = ref (n + 1) in
|
|
let sum = ref 0 in
|
|
let l = ref 0 in
|
|
for r = 0 to n - 1 do
|
|
sum := !sum + arr.(r);
|
|
while !sum >= target do
|
|
let len = r - !l + 1 in
|
|
if len < !best then best := len;
|
|
sum := !sum - arr.(!l);
|
|
l := !l + 1
|
|
done
|
|
done;
|
|
if !best > n then 0 else !best
|
|
|
|
;;
|
|
|
|
min_subarr_sum_at_least [| 2; 3; 1; 2; 4; 3 |] 7
|