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)]
