LeetCode 1396. Design Underground System in F#

URL

leetcode.com/problems/design-underground-sy..

Code

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

type UndergroundSystem =
    { mutable Check: Map<int, (string * float)>
      mutable Time: Map<(string * string), (int * float)> }

    static member init() = { Check = Map.empty; Time = Map.empty }

    member this.checkIn (id: int) (stationName: string) (t: int) =
        this.Check <- Map.add id (stationName, float t) this.Check

    member this.checkOut (id: int) (stationName: string) (t: int) : unit =
        match Map.tryFind id this.Check with
        | None -> failwith "never reach here"
        | Some ((start, startTime)) ->
            let diff = (float t) - startTime
            let key = (start, stationName)

            match Map.tryFind key this.Time with
            | None -> this.Time <- Map.add key (1, diff) this.Time
            | Some ((num, total)) -> this.Time <- Map.add key (num + 1, total + diff) this.Time


    member this.getAverageTime (startStation: string) (endStation: string) : float =
        let key = (startStation, endStation)
        let (num, total) = Map.find key this.Time
        total / (float num)