LeetCode 1679. Max Number of K-Sum Pairs in F#

URL

leetcode.com/problems/max-number-of-k-sum-p..

Code

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

let maxOperations (nums: int list) (k: int) : int =
    let rec maxOperations' nums k m ret =
        match nums with
        | [] -> ret
        | h :: t ->
            let diff = k - h

            match Map.tryFind diff m with
            | Some (v) when v >= 1 -> maxOperations' t k (Map.add diff (v - 1) m) (ret + 1)
            | _ ->
                match Map.tryFind h m with
                | None -> maxOperations' t k (Map.add h 1 m) ret
                | Some (w) -> maxOperations' t k (Map.add h (w + 1) m) ret

    maxOperations' nums k Map.empty 0