Skip to content

Instantly share code, notes, and snippets.

@prepor
Created April 6, 2018 08:58
Show Gist options
  • Save prepor/3627ae3f748ddb1325708cc029b0b4fe to your computer and use it in GitHub Desktop.
Save prepor/3627ae3f748ddb1325708cc029b0b4fe to your computer and use it in GitHub Desktop.
open Base
let max_population intervals =
let t = Hashtbl.create (module Int) in
List.iter intervals (fun (birth, dead) ->
Hashtbl.update t birth (function | None -> 1 | Some i -> i + 1);
Hashtbl.update t dead (function | None -> -1 | Some i -> i - 1));
let years = List.sort ~compare:Int.compare (Hashtbl.keys t) in
let max = ref 0 in
let current = ref 0 in
List.iter years (fun y ->
current := !current + Hashtbl.find_exn t y;
if !current > !max then max := !current);
!max
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment