From 0231bb46a6824260c2a1da50bb91b1968152a86c Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 05:54:39 +0000 Subject: [PATCH] ocaml: phase 5.1 trapping_rain.ml baseline (LeetCode trapped water = 6) Classic trapped-rain-water two-pass DP: left_max[i] = max(heights[0..i]) (forward sweep) right_max[i] = max(heights[i..n-1]) (downto sweep) water = sum over i of (min(left_max[i], right_max[i]) - heights[i]) For [0; 1; 0; 2; 1; 0; 1; 3; 2; 1; 2; 1]: water = 6. Tests dual sweep (forward + downto), array of running maxes, inline-if rhs of <- for running-max update (uses iter-236 fix for <- accepting if/match RHS). 203 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/trapping_rain.ml | 25 +++++++++++++++++++++++++ plans/ocaml-on-sx.md | 6 ++++++ 3 files changed, 32 insertions(+) create mode 100644 lib/ocaml/baseline/trapping_rain.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 467d28c6..4330a942 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -194,6 +194,7 @@ "sieve.ml": 15, "sum_squares.ml": 385, "tree_depth.ml": 4, + "trapping_rain.ml": 6, "triangle.ml": 11, "trie.ml": 6, "triangle_div.ml": 120, diff --git a/lib/ocaml/baseline/trapping_rain.ml b/lib/ocaml/baseline/trapping_rain.ml new file mode 100644 index 00000000..19b4e8a4 --- /dev/null +++ b/lib/ocaml/baseline/trapping_rain.ml @@ -0,0 +1,25 @@ +let trap heights = + let n = Array.length heights in + if n < 3 then 0 + else begin + let left_max = Array.make n 0 in + let right_max = Array.make n 0 in + left_max.(0) <- heights.(0); + for i = 1 to n - 1 do + left_max.(i) <- if heights.(i) > left_max.(i - 1) then heights.(i) else left_max.(i - 1) + done; + right_max.(n - 1) <- heights.(n - 1); + for i = n - 2 downto 0 do + right_max.(i) <- if heights.(i) > right_max.(i + 1) then heights.(i) else right_max.(i + 1) + done; + let water = ref 0 in + for i = 0 to n - 1 do + let min_lr = if left_max.(i) < right_max.(i) then left_max.(i) else right_max.(i) in + water := !water + min_lr - heights.(i) + done; + !water + end + +;; + +trap [| 0; 1; 0; 2; 1; 0; 1; 3; 2; 1; 2; 1 |] diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index c5c26809..889040fc 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,12 @@ _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 — trapping_rain.ml baseline (trapped rain + water over heights [0;1;0;2;1;0;1;3;2;1;2;1] = 6). Left-max and + right-max prefix arrays; at each index water = min(L,R) − h. + Tests dual sweep (forward + downto), array of running maxes, + inline-if rhs of `<-` for running-max update (uses iter-236 + fix). 203 baseline programs total. - 2026-05-11 Phase 5.1 — gas_station.ml baseline (find unique start station for circular gas tour, gas=[1;2;3;4;5] cost=[3;4;5;1;2] → start at index 3). Classic O(n) greedy: walk once tracking the