diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index 3fc5f364..19c2600c 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -75,6 +75,7 @@ "hamming.ml": 4, "hanoi.ml": 1023, "hist.ml": 75, + "histogram_area.ml": 10, "huffman.ml": 224, "int_sqrt.ml": 1027, "is_prime.ml": 25, diff --git a/lib/ocaml/baseline/histogram_area.ml b/lib/ocaml/baseline/histogram_area.ml new file mode 100644 index 00000000..5edb7d7e --- /dev/null +++ b/lib/ocaml/baseline/histogram_area.ml @@ -0,0 +1,27 @@ +let max_rect heights = + let n = Array.length heights in + let stack = ref [] in + let best = ref 0 in + for i = 0 to n do + let h = if i = n then 0 else heights.(i) in + let cont = ref true in + while !cont do + match !stack with + | top :: rest when heights.(top) > h -> + let height = heights.(top) in + stack := rest; + let width = match !stack with + | [] -> i + | t :: _ -> i - t - 1 + in + let area = height * width in + if area > !best then best := area + | _ -> cont := false + done; + if i < n then stack := i :: !stack + done; + !best + +;; + +max_rect [| 2; 1; 5; 6; 2; 3 |] diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 28ae44c5..99f48089 100644 --- a/plans/ocaml-on-sx.md +++ b/plans/ocaml-on-sx.md @@ -407,6 +407,15 @@ _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 — histogram_area.ml baseline (largest + rectangle in histogram [2;1;5;6;2;3] = 10). Linear-time stack + algorithm: push indices while heights are non-decreasing; on + drop, pop and compute the bar's max-width rectangle. Sentinel + iteration at i=n with h=0 flushes remaining stack. The bars at + indices 2 and 3 (heights 5 and 6) form a width-2 rectangle of + height 5 = 10. Tests `match … with | top :: rest when h > … -> … + | _ -> …` guarded patterns inside a while-cont-flag loop, nested + match on the same stack ref. 178 baseline programs total. - 2026-05-11 Phase 5.1 — lps_dp.ml baseline (longest palindromic subsequence in "BBABCBCAB" = 7). Classic 2D O(n²) DP over substring lengths: dp[i][j] = dp[i+1][j-1] + 2 when s[i]=s[j],