From bf468e5ec393bbd39da94a73ae3f85f230767db5 Mon Sep 17 00:00:00 2001 From: giles Date: Mon, 11 May 2026 04:14:33 +0000 Subject: [PATCH] ocaml: phase 5.1 min_meeting_rooms.ml baseline (8 meetings, min 4 rooms) Sweep-line algorithm via separately-sorted starts / ends arrays: while i < n do if starts[i] < ends[j] then begin busy++; rooms = max; i++ end else begin busy--; j++ end done intervals: (0,30) (5,10) (15,20) (10,25) (5,12) (20,35) (0,5) (8,18) At time 8, meetings (0,30), (5,10), (5,12), (8,18) are all active simultaneously -> answer = 4. Tests local helper bound via let (`let bubble a = ...`) for in-place sort, dual-pointer sweep on parallel ordered event streams. 193 baseline programs total. --- lib/ocaml/baseline/expected.json | 1 + lib/ocaml/baseline/min_meeting_rooms.ml | 40 +++++++++++++++++++++++++ plans/ocaml-on-sx.md | 9 ++++++ 3 files changed, 50 insertions(+) create mode 100644 lib/ocaml/baseline/min_meeting_rooms.ml diff --git a/lib/ocaml/baseline/expected.json b/lib/ocaml/baseline/expected.json index b98d1d25..908d4325 100644 --- a/lib/ocaml/baseline/expected.json +++ b/lib/ocaml/baseline/expected.json @@ -121,6 +121,7 @@ "mortgage.ml": 1073, "mst_kruskal.ml": 11, "merge_intervals.ml": 12, + "min_meeting_rooms.ml": 4, "merge_sort.ml": 44, "merge_two.ml": 441, "min_cost_path.ml": 12, diff --git a/lib/ocaml/baseline/min_meeting_rooms.ml b/lib/ocaml/baseline/min_meeting_rooms.ml new file mode 100644 index 00000000..2a4741ba --- /dev/null +++ b/lib/ocaml/baseline/min_meeting_rooms.ml @@ -0,0 +1,40 @@ +let min_rooms intervals = + let n = List.length intervals in + let arr = Array.of_list intervals in + let starts = Array.make n 0 in + let ends = Array.make n 0 in + for i = 0 to n - 1 do + let (s, e) = arr.(i) in + starts.(i) <- s; + ends.(i) <- e + done; + let bubble a = + for i = 0 to n - 1 do + for j = 0 to n - 2 - i do + if a.(j) > a.(j + 1) then begin + let t = a.(j) in + a.(j) <- a.(j + 1); + a.(j + 1) <- t + end + done + done + in + bubble starts; + bubble ends; + let rooms = ref 0 in + let busy = ref 0 in + let i = ref 0 and j = ref 0 in + while !i < n do + if starts.(!i) < ends.(!j) then begin + busy := !busy + 1; + if !busy > !rooms then rooms := !busy; + i := !i + 1 + end else begin + busy := !busy - 1; + j := !j + 1 + end + done; + !rooms +;; + +min_rooms [(0, 30); (5, 10); (15, 20); (10, 25); (5, 12); (20, 35); (0, 5); (8, 18)] diff --git a/plans/ocaml-on-sx.md b/plans/ocaml-on-sx.md index 1b017130..1adef748 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 — 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: + on start