Created
June 29, 2017 20:56
-
-
Save bjartwolf/2a9b534192f7575e5f6c83c1c874f2b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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