Shohei Yoshida
Shohei Yoshida's Blog

Shohei Yoshida's Blog

LeetCode 729. My Calendar I in F#

Shohei Yoshida's photo
Shohei Yoshida
·Aug 3, 2022·

1 min read

URL

leetcode.com/problems/my-calendar-i

Code

github.com/syohex/dotnet-study/blob/master/..

type MyCalendar =
    { Ranges: (int * int) list }

    static member init() : MyCalendar = { Ranges = [] }

    static member book (start: int) (finish: int) (cal: MyCalendar) : (bool * MyCalendar) =
        let rec book' ranges start finish =
            match ranges with
            | [] -> true, { cal with Ranges = (start, finish) :: cal.Ranges }
            | (s, e) :: rest ->
                if s < finish && start < e then
                    false, cal
                else
                    book' rest start finish

        book' cal.Ranges start finish
 
Share this