LeetCode 1423. Maximum Points You Can Obtain from Cards in F#

URL

leetcode.com/problems/maximum-points-you-ca..

Code

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

open System

let maxScore (cardPoints: int list) (k: int) : int =
    let rec maxScore' cards revCards sum (ret: int) =
        match cards, revCards with
        | [], [] -> ret
        | [], _
        | _, [] -> failwith "never reach here"
        | h1 :: t1, h2 :: t2 ->
            let sum' = sum + h1 - h2
            maxScore' t1 t2 sum' (Math.Max(ret, sum'))

    let cards = cardPoints |> List.take k
    let revCards = cardPoints |> List.rev |> List.take k |> List.rev
    let sum = revCards |> List.sum
    maxScore' cards revCards sum sum