URL
My Calendar II - LeetCode
Code
https://github.com/syohex/dotnet-study/tree/master/fsharp/leetcode/challenge/202409/my_calendar2/main.fsx
type MyCalendarTwo =
{ Bookings: (int * int) list
Overlapped: (int * int) list }
static member Empty: MyCalendarTwo = { Bookings = []; Overlapped = [] }
static member Book (start': int) (end': int) (c: MyCalendarTwo) : bool * MyCalendarTwo =
let isOverlapped (s1, e1) (s2, e2) = (max s1 s2) < (min e1 e2)
let rec isTripleBookings overlapped =
match overlapped with
| [] -> false
| range :: t ->
if isOverlapped range (start', end') then
true
else
isTripleBookings t
let rec checkOverlapped bookings c =
match bookings with
| [] -> c
| (s, e) :: t ->
if isOverlapped (s, e) (start', end') then
checkOverlapped
t
{ c with
Overlapped = (max s start', min e end') :: c.Overlapped }
else
checkOverlapped t c
if isTripleBookings c.Overlapped then
false, c
else
let c' = checkOverlapped c.Bookings c
true,
{ c' with
Bookings = (start', end') :: c.Bookings }