Skip to content

Instantly share code, notes, and snippets.

@bjartwolf
Created June 29, 2017 20:56
Show Gist options
  • Save bjartwolf/2a9b534192f7575e5f6c83c1c874f2b8 to your computer and use it in GitHub Desktop.
Save bjartwolf/2a9b534192f7575e5f6c83c1c874f2b8 to your computer and use it in GitHub Desktop.
type OpenInterval = {Start: int; End: int }
let sorted = [{Start = 10; End = 19};
{Start = 11; End = 19};
{Start = 0; End = 8}] |> Seq.sortBy (fun f -> f.Start)
let stack = new Stack<OpenInterval>()
stack.Push (Seq.head sorted)
for interval in Seq.tail sorted do
let overLap i i' = i.End + 1 >= i'.Start
if not (overLap (stack.Peek()) interval) then
stack.Push interval
else if (overLap (stack.Peek()) interval) && interval.End > (stack.Peek()).End then
stack.Push({ (stack.Pop()) with End = interval.End })
stack.Sum(System.Func<OpenInterval,int>(fun i -> i.End - i.Start)) |> printf "%A"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment