From 33be068c01e20f743dd3bbe171f29ef1c8f70347 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 04:24:29 +0000 Subject: [PATCH] ocaml: phase 5.1 min_subarr_target.ml baseline (min subarray sum >= 7 = 2) 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. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/min_subarr_target.ml | 19 +++++++++++++++++++ plans/ocaml-on-sx.md | 8 ++++++++ 3 files changed, 28 insertions(+) create mode 100644 lib/ocaml/baseline/min_subarr_target.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 908d4325..7c10507e 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -125,6 +125,7 @@ "merge_sort.ml": 44, "merge_two.ml": 441, "min_cost_path.ml": 12, + "min_subarr_target.ml": 2, "module_use.ml": 3, "monotonic.ml": 4, "newton_sqrt.ml": 1414, diff --git a/lib/ocaml/baseline/min_subarr_target.ml b/lib/ocaml/baseline/min_subarr_target.ml new file mode 100644 index 00000000..14469291 --- /dev/null +++ b/lib/ocaml/baseline/min_subarr_target.ml @@ -0,0 +1,19 @@ +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 diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 1adef748..d1603223 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,14 @@ _Newest first._ binary search tree (`type 'a tree = Leaf | Node of 'a * 'a tree * 'a tree`) with insert + in-order traversal. Tests parametric ADT, recursive match, List.append, List.fold_left. +- 2026-05-11 Phase 5.1 — min_subarr_target.ml baseline (sliding- + window min subarray with sum ≥ target on [2;3;1;2;4;3] target=7 + = 2). Two-pointer: expand right, then shrink left while sum + stays ≥ target, recording the min length seen. Optimal window + is [4, 3] (positions 4-5) with sum 7, length 2. Tests `for` + + inner `while` shrinking loop, ref-tracked sum updated on both + expansion and contraction, sentinel `n + 1` for "not found". + 194 baseline programs total. - 2026-05-11 Phase 5.1 — min_meeting_rooms.ml baseline (sweep-line for min concurrent meetings on 8 intervals = 4). Separate starts and ends arrays sorted independently, then a two-pointer sweep: