let task_intervals tasks n =
  let counts = Array.make 26 0 in
  String.iter (fun c -> counts.(Char.code c - Char.code 'A') <- counts.(Char.code c - Char.code 'A') + 1) tasks;
  let max_c = ref 0 in
  for i = 0 to 25 do
    if counts.(i) > !max_c then max_c := counts.(i)
  done;
  let max_n = ref 0 in
  for i = 0 to 25 do
    if counts.(i) = !max_c then max_n := !max_n + 1
  done;
  let intervals = (!max_c - 1) * (n + 1) + !max_n in
  let total = String.length tasks in
  if intervals > total then intervals else total

;;

task_intervals "AAABBC" 2
